From 5e574c5070359795e855713abc0a697dd2d85017 Mon Sep 17 00:00:00 2001 From: Remo Fritzsche Date: Tue, 25 Jul 2023 10:52:23 +0200 Subject: [PATCH] Make failed sub operations result in error 500 when called via controller --- CHANGELOG.md | 11 +++++++++++ lib/rails_ops/controller_mixin.rb | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1060b6..f6a3140 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,25 @@ # Changelog +## 1.4.5 (2023-07-25) + +* Make sure that `RailsOps::Exceptions::SubOpValidationFailed` always results in + an Error 500 when handled by a Rails controller. + + Internal reference: `#114719`. + ## 1.4.5 (2023-07-10) * Fix bug introduced in previous release 1.4.4. + Internal reference: `#114719`. + ## 1.4.4 (2023-07-10) * Adapt method `sub_op` to catch `.validation_errors` and re-throw them as `RailsOps::Exceptions::SubOpValidationFailed`. + Internal reference: `#114719`. + ## 1.4.3 (2023-03-27) * Extend the `operation` generator to accept additional flags to skip the diff --git a/lib/rails_ops/controller_mixin.rb b/lib/rails_ops/controller_mixin.rb index 5f4b873..2057d2e 100644 --- a/lib/rails_ops/controller_mixin.rb +++ b/lib/rails_ops/controller_mixin.rb @@ -47,6 +47,17 @@ module ControllerMixin fail ActionController::BadRequest end end + + # #114719: If 'rescue_validation_error_in_controller' is enabled, the + # exception 'SubOpValidationFailed' may have + # 'Schemacop::Exceptions::ValidationError' as a 'cause', which in turn + # causes ActiveSupport's 'Rescuable' to look for a rescue_from with the + # exception's 'cause', which would then result in a BadRequest. For this + # case, we add a separate rescue_from here so that 'SubOpValidationFailed' + # always results in an error 500 as it must be a programming error. + rescue_from RailsOps::Exceptions::SubOpValidationFailed do |e| + fail e + end end # Instantiates and returns a new operation with the given class. If no class