diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb new file mode 100644 index 000000000..394bbd89e --- /dev/null +++ b/app/controllers/errors_controller.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true +class ErrorsController < ApplicationController + skip_before_action :authenticate_user! + + def not_found + render(status: :not_found) + end + + def internal_server_error + render(status: :internal_server_error) + end +end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 9803fe013..fc504ea9d 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -164,8 +164,10 @@ def assign_curator render json: {} end rescue => ex + # This is necessary for JSON responses Rails.logger.error("Error changing curator for work: #{work.id}. Exception: #{ex.message}") - render json: { errors: ["Cannot save dataset"] }, status: :bad_request + Honeybadger.notify("Error changing curator for work: #{work.id}. Exception: #{ex.message}") + render(json: { errors: ["Cannot save dataset"] }, status: :bad_request) end def add_message @@ -314,10 +316,15 @@ def rescue_aasm_error if action_name == "create" handle_error_for_create(generic_error) else - redirect_to root_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators." + redirect_to error_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators." end end + rescue_from StandardError do |generic_error| + Honeybadger.notify("We apologize, an error was encountered: #{generic_error.message}.") + redirect_to error_url, notice: "We apologize, an error was encountered: #{generic_error.message}. Please contact the PDC Describe administrators." + end + # @note No testing coverage but not a route, not called def handle_error_for_create(generic_error) if @work.persisted? diff --git a/app/views/errors/internal_server_error.html.erb b/app/views/errors/internal_server_error.html.erb new file mode 100644 index 000000000..4ed53f95a --- /dev/null +++ b/app/views/errors/internal_server_error.html.erb @@ -0,0 +1,2 @@ +
For assistance, please e-mail prds@princeton.edu for support.
diff --git a/app/views/errors/not_found.html.erb b/app/views/errors/not_found.html.erb new file mode 100644 index 000000000..b4703ae27 --- /dev/null +++ b/app/views/errors/not_found.html.erb @@ -0,0 +1,2 @@ +For assistance, please e-mail prds@princeton.edu for support.
diff --git a/config/application.rb b/config/application.rb index 3fec3d391..d706ad4b6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -33,5 +33,7 @@ class Application < Rails::Application # Explicitly set timezome rather than relying on system, # which may be different in CI environment. config.time_zone = "America/New_York" + + config.exceptions_app = routes end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 5232f6e48..7c6216c04 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -16,7 +16,7 @@ config.sass.cache = false # Show full error reports. - config.consider_all_requests_local = true + config.consider_all_requests_local = false # Enable/disable caching. By default caching is disabled. # Run rails dev:cache to toggle caching. diff --git a/config/environments/test.rb b/config/environments/test.rb index d15f17917..38c3f9a0c 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -29,7 +29,7 @@ } # Show full error reports and disable caching. - config.consider_all_requests_local = true + config.consider_all_requests_local = false config.action_controller.perform_caching = false config.cache_store = :null_store diff --git a/config/routes.rb b/config/routes.rb index ddaaa1f18..4a48f4f51 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -96,4 +96,7 @@ # Anything still unmatched by the end of the routes file should go to the not_found page # match '*a', to: redirect('/404'), via: :get + + match "/404", to: "errors#not_found", via: :all, as: :not_found + match "/500", to: "errors#internal_server_error", via: :all, as: :error end diff --git a/public/404.html b/public/404.html deleted file mode 100644 index 2be3af26f..000000000 --- a/public/404.html +++ /dev/null @@ -1,67 +0,0 @@ - - - -You may have mistyped the address or the page may have moved.
-If you are the application owner check the logs for more information.
-If you are the application owner check the logs for more information.
-