From 80cfe6fa09b076ced4386e92153479195dc9dd6e Mon Sep 17 00:00:00 2001 From: Raul Riera Date: Tue, 18 Aug 2026 11:00:20 -0400 Subject: [PATCH] fix(errors): render a dedicated 422 page instead of the 500 page /422 was routed to errors#server_error, so unprocessable-entity responses masqueraded as server errors. Add an unprocessable action, template and translated message in en/es/pt, with status tests for all four error pages. --- app/controllers/errors_controller.rb | 4 ++++ app/views/errors/unprocessable.html.erb | 14 +++++++++++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/pt.yml | 1 + config/routes.rb | 2 +- test/functional/errors_controller_test.rb | 29 +++++++++++++++++++++++ 7 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/views/errors/unprocessable.html.erb create mode 100644 test/functional/errors_controller_test.rb diff --git a/app/controllers/errors_controller.rb b/app/controllers/errors_controller.rb index ec155ae4..e3dc1aea 100644 --- a/app/controllers/errors_controller.rb +++ b/app/controllers/errors_controller.rb @@ -15,4 +15,8 @@ def server_error def unauthorised render status: 401, formats: [:html] end + + def unprocessable + render status: 422, formats: [:html] + end end diff --git a/app/views/errors/unprocessable.html.erb b/app/views/errors/unprocessable.html.erb new file mode 100644 index 00000000..e8f02f1b --- /dev/null +++ b/app/views/errors/unprocessable.html.erb @@ -0,0 +1,14 @@ +
+
422
+

<%= t "errors.titles.form_update" %>

+

+ <%= t "errors.messages.unprocessable" %> +

+ +
diff --git a/config/locales/en.yml b/config/locales/en.yml index 1699ebc2..fd6bc7cd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -213,6 +213,7 @@ en: go_home: Go home messages: invalid_date_range: The chosen dates are invalid + unprocessable: We couldn't process your request. Please go back and try again. different_practice: Must belong to this practice invalid_email_format: Invalid format unauthorised: You don't have permission to do this diff --git a/config/locales/es.yml b/config/locales/es.yml index 331d07e7..7c8b9bb9 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -209,6 +209,7 @@ es: go_home: Ir a la página principal messages: invalid_date_range: Las fechas son invalidas. + unprocessable: No pudimos procesar tu solicitud. Por favor regresa e inténtalo de nuevo. different_practice: Debe pertenecer a esta clínica invalid_email_format: Formato invalido. unauthorised: No posees los permisos necesarios para hacer esto. diff --git a/config/locales/pt.yml b/config/locales/pt.yml index 68ecc059..9009efc6 100644 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -211,6 +211,7 @@ pt: go_home: Ir para início messages: invalid_date_range: As datas escolhidas são inválidas + unprocessable: Não conseguimos processar sua solicitação. Por favor, volte e tente novamente. different_practice: Deve pertencer a esta clínica invalid_email_format: Formato inválido unauthorised: Você não tem permissão para fazer isso diff --git a/config/routes.rb b/config/routes.rb index d1f32049..af9b10ba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -114,7 +114,7 @@ # error handling get '/404', to: 'errors#not_found' get '/401', to: 'errors#unauthorised' - get '/422', to: 'errors#server_error' + get '/422', to: 'errors#unprocessable' get '/500', to: 'errors#server_error' root to: 'welcome#index' diff --git a/test/functional/errors_controller_test.rb b/test/functional/errors_controller_test.rb new file mode 100644 index 00000000..003157bc --- /dev/null +++ b/test/functional/errors_controller_test.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'test_helper' + +class ErrorsControllerTest < ActionController::TestCase + test '/422 routes to the unprocessable page' do + assert_routing '/422', controller: 'errors', action: 'unprocessable' + end + + test 'unprocessable renders with a 422 status' do + get :unprocessable + assert_response :unprocessable_entity + end + + test 'not_found renders with a 404 status' do + get :not_found + assert_response :not_found + end + + test 'unauthorised renders with a 401 status' do + get :unauthorised + assert_response :unauthorized + end + + test 'server_error renders with a 500 status' do + get :server_error + assert_response :internal_server_error + end +end