From 349b542c0e6828d954039383c298111a7f85bad2 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 14 Dec 2025 01:48:11 -0800 Subject: [PATCH] fix rescue_from order so StandardError is checked last --- app/controllers/api/v1/api_controller.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb index fe77ded..1efc0bf 100644 --- a/app/controllers/api/v1/api_controller.rb +++ b/app/controllers/api/v1/api_controller.rb @@ -13,6 +13,14 @@ module Api MIN_PER_PAGE = 1 ##### Errors + # Catch-all for unhandled exceptions - log details and return 500 + # NOTE: Must be defined FIRST so it's checked LAST (Rails matches bottom-to-top) + rescue_from StandardError do |e| + Rails.logger.error "[500 Error] #{e.class}: #{e.message}" + Rails.logger.error e.backtrace&.first(20)&.join("\n") + render json: { error: 'Internal Server Error', message: e.message }, status: :internal_server_error + end + rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response_without_object @@ -44,13 +52,6 @@ module Api render_error(e) end - # Catch-all for unhandled exceptions - log details and return 500 - rescue_from StandardError do |e| - Rails.logger.error "[500 Error] #{e.class}: #{e.message}" - Rails.logger.error e.backtrace&.first(20)&.join("\n") - render json: { error: 'Internal Server Error', message: e.message }, status: :internal_server_error - end - ##### Hooks before_action :current_user before_action :default_content_type