From 62997ecc0b0423a7e86a939b68cb3138dca67682 Mon Sep 17 00:00:00 2001 From: Karel Picman Date: Tue, 1 Mar 2016 13:15:58 +0100 Subject: [PATCH] Better exception handling when a file is physically missing --- app/controllers/dmsf_controller.rb | 20 ++++-------- lib/dmsf_zip.rb | 7 ++-- lib/redmine_dmsf.rb | 12 ++++--- lib/redmine_dmsf/errors.rb | 28 ++++++++++++++++ .../errors}/dmsf_access_error.rb | 5 ++- .../errors}/dmsf_content_error.rb | 5 ++- .../errors/dmsf_email_max_file_error.rb | 32 +++++++++++++++++++ .../errors/dmsf_file_not_found_error.rb | 23 +++++++++++++ .../errors}/dmsf_lock_error.rb | 5 ++- .../errors/dmsf_zip_max_file_error.rb | 32 +++++++++++++++++++ lib/redmine_dmsf/webdav/download.rb | 2 +- 11 files changed, 148 insertions(+), 23 deletions(-) create mode 100644 lib/redmine_dmsf/errors.rb rename lib/{ => redmine_dmsf/errors}/dmsf_access_error.rb (85%) rename lib/{ => redmine_dmsf/errors}/dmsf_content_error.rb (85%) create mode 100644 lib/redmine_dmsf/errors/dmsf_email_max_file_error.rb create mode 100644 lib/redmine_dmsf/errors/dmsf_file_not_found_error.rb rename lib/{ => redmine_dmsf/errors}/dmsf_lock_error.rb (84%) create mode 100644 lib/redmine_dmsf/errors/dmsf_zip_max_file_error.rb diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index c33e38a0..13895578 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -23,10 +23,6 @@ class DmsfController < ApplicationController unloadable - class ZipMaxFilesError < StandardError; end - class EmailMaxFileSize < StandardError; end - class FileNotFound < StandardError; end - before_filter :find_project before_filter :authorize before_filter :find_folder, :except => [:new, :create, :edit_root, :save_root] @@ -217,17 +213,15 @@ class DmsfController < ApplicationController redirect_to :back else download_entries(selected_folders, selected_files) - end - rescue ZipMaxFilesError - flash[:error] = l(:error_max_files_exceeded, :number => Setting.plugin_redmine_dmsf['dmsf_max_file_download']) - redirect_to :back - rescue EmailMaxFileSize - flash[:error] = l(:error_max_email_filesize_exceeded, :number => Setting.plugin_redmine_dmsf['dmsf_max_email_filesize']) - redirect_to :back + end rescue FileNotFound render_404 rescue DmsfAccessError - render_403 + render_403 + rescue Exception => e + flash[:error] = e.message + Rails.logger.error e.message + redirect_to :back end def tag_changed @@ -518,7 +512,7 @@ class DmsfController < ApplicationController end max_files = Setting.plugin_redmine_dmsf['dmsf_max_file_download'].to_i if max_files > 0 && zip.files.length > max_files - raise ZipMaxFilesError, zip.files.length + raise ZipMaxFilesError#, zip.files.length end zip end diff --git a/lib/dmsf_zip.rb b/lib/dmsf_zip.rb index 477345fd..088148f1 100644 --- a/lib/dmsf_zip.rb +++ b/lib/dmsf_zip.rb @@ -1,6 +1,9 @@ +# encoding: utf-8 +# # Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011-16 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -60,4 +63,4 @@ class DmsfZip folder.files.visible.each { |file| self.add_file(file, member, root_path) } end -end +end \ No newline at end of file diff --git a/lib/redmine_dmsf.rb b/lib/redmine_dmsf.rb index 4c6d70eb..4ce45298 100644 --- a/lib/redmine_dmsf.rb +++ b/lib/redmine_dmsf.rb @@ -1,8 +1,10 @@ +# encoding: utf-8 +# # Redmine plugin for Document Management System "Features" # # Copyright (C) 2011 Vít Jonáš # Copyright (C) 2012 Daniel Munn -# Copyright (C) 2011-14 Karel Pičman +# Copyright (C) 2011-16 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License @@ -21,8 +23,9 @@ DMSF_MAX_NOTIFICATION_RECEIVERS_INFO = 10 # DMSF libraries -require 'redmine_dmsf/patches' #plugin patches -require 'redmine_dmsf/webdav' #DAV4Rack implementation +require 'redmine_dmsf/patches' # plugin patches +require 'redmine_dmsf/webdav' # DAV4Rack implementation +require 'redmine_dmsf/errors' # Exceptions # Hooks @@ -33,4 +36,5 @@ module RedmineDmsf end # Add the plugin view folder into ActionMailer's paths to search -ActionMailer::Base.append_view_path(File.expand_path(File.dirname(__FILE__) + '/../app/views')) \ No newline at end of file +ActionMailer::Base.append_view_path(File.expand_path( + File.dirname(__FILE__) + '/../app/views')) \ No newline at end of file diff --git a/lib/redmine_dmsf/errors.rb b/lib/redmine_dmsf/errors.rb new file mode 100644 index 00000000..97fcdece --- /dev/null +++ b/lib/redmine_dmsf/errors.rb @@ -0,0 +1,28 @@ +# encoding: utf-8 +# +# Redmine plugin for Document Management System "Features" +# +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011-16 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require 'redmine_dmsf/errors/dmsf_access_error.rb' +require 'redmine_dmsf/errors/dmsf_content_error.rb' +require 'redmine_dmsf/errors/dmsf_email_max_file_error.rb' +require 'redmine_dmsf/errors/dmsf_file_not_found_error.rb' +require 'redmine_dmsf/errors/dmsf_lock_error.rb' +require 'redmine_dmsf/errors/dmsf_zip_max_file_error.rb' \ No newline at end of file diff --git a/lib/dmsf_access_error.rb b/lib/redmine_dmsf/errors/dmsf_access_error.rb similarity index 85% rename from lib/dmsf_access_error.rb rename to lib/redmine_dmsf/errors/dmsf_access_error.rb index 98d54324..3131e5ba 100644 --- a/lib/dmsf_access_error.rb +++ b/lib/redmine_dmsf/errors/dmsf_access_error.rb @@ -1,6 +1,9 @@ +# encoding: utf-8 +# # Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011-16 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License diff --git a/lib/dmsf_content_error.rb b/lib/redmine_dmsf/errors/dmsf_content_error.rb similarity index 85% rename from lib/dmsf_content_error.rb rename to lib/redmine_dmsf/errors/dmsf_content_error.rb index 2cebdffc..1e73e9a2 100644 --- a/lib/dmsf_content_error.rb +++ b/lib/redmine_dmsf/errors/dmsf_content_error.rb @@ -1,6 +1,9 @@ +# encoding: utf-8 +# # Redmine plugin for Document Management System "Features" # -# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011 Vít Jonáš +# Copyright (C) 2011-16 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License diff --git a/lib/redmine_dmsf/errors/dmsf_email_max_file_error.rb b/lib/redmine_dmsf/errors/dmsf_email_max_file_error.rb new file mode 100644 index 00000000..5ca70174 --- /dev/null +++ b/lib/redmine_dmsf/errors/dmsf_email_max_file_error.rb @@ -0,0 +1,32 @@ +# encoding: utf-8 +# +# Redmine plugin for Document Management System "Features" +# +# Copyright (C) 2011-16 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class EmailMaxFileSize < StandardError + include Redmine::I18n + + def initialize(message = nil) + if message.present? + super message + else + super l(:error_max_email_filesize_exceeded, + :number => Setting.plugin_redmine_dmsf['dmsf_max_email_filesize']) + end + end +end diff --git a/lib/redmine_dmsf/errors/dmsf_file_not_found_error.rb b/lib/redmine_dmsf/errors/dmsf_file_not_found_error.rb new file mode 100644 index 00000000..b55d226e --- /dev/null +++ b/lib/redmine_dmsf/errors/dmsf_file_not_found_error.rb @@ -0,0 +1,23 @@ +# encoding: utf-8 +# +# Redmine plugin for Document Management System "Features" +# +# Copyright (C) 2011-16 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class FileNotFound < StandardError + +end diff --git a/lib/dmsf_lock_error.rb b/lib/redmine_dmsf/errors/dmsf_lock_error.rb similarity index 84% rename from lib/dmsf_lock_error.rb rename to lib/redmine_dmsf/errors/dmsf_lock_error.rb index b74493c5..0ccc326a 100644 --- a/lib/dmsf_lock_error.rb +++ b/lib/redmine_dmsf/errors/dmsf_lock_error.rb @@ -1,6 +1,9 @@ +# encoding: utf-8 +# # Redmine plugin for Document Management System "Features" # -# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2012 Daniel Munn +# Copyright (C) 2011-16 Karel Pičman # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License diff --git a/lib/redmine_dmsf/errors/dmsf_zip_max_file_error.rb b/lib/redmine_dmsf/errors/dmsf_zip_max_file_error.rb new file mode 100644 index 00000000..c888c486 --- /dev/null +++ b/lib/redmine_dmsf/errors/dmsf_zip_max_file_error.rb @@ -0,0 +1,32 @@ +# encoding: utf-8 +# +# Redmine plugin for Document Management System "Features" +# +# Copyright (C) 2011-16 Karel Pičman +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +class ZipMaxFilesError < StandardError + include Redmine::I18n + + def initialize(message = nil) + if message.present? + super message + else + super l(:error_max_files_exceeded, + :number => Setting.plugin_redmine_dmsf['dmsf_max_file_download']) + end + end +end diff --git a/lib/redmine_dmsf/webdav/download.rb b/lib/redmine_dmsf/webdav/download.rb index f7e2698d..4a8c0dbf 100644 --- a/lib/redmine_dmsf/webdav/download.rb +++ b/lib/redmine_dmsf/webdav/download.rb @@ -45,7 +45,7 @@ module RedmineDmsf if available serving(env) else - raise NotFound + return fail(403, 'Not Found') end end end