From 7b3d8e0c13e380e3fd2576f4bfb5d64b69ce38d6 Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Fri, 2 Mar 2018 00:52:51 +0900 Subject: [PATCH] Rescue truly all exceptions --- CHANGELOG.md | 1 + README.md | 4 ++-- lib/grape/middleware/error.rb | 2 +- spec/grape/middleware/exception_spec.rb | 9 +++++++-- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7380800db9..ac773ae8ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * [#1752](https://github.com/ruby-grape/grape/pull/1752): Fix `include_missing` behavior for aliased parameters - [@jonasoberschweiber](https://github.com/jonasoberschweiber). * [#1754](https://github.com/ruby-grape/grape/pull/1754): Allow rescue from non-`StandardError` exceptions to use default error handling - [@jelkster](https://github.com/jelkster). * [#1756](https://github.com/ruby-grape/grape/pull/1756): Allow custom Grape exception handlers when the built-in exception handling is enabled - [@soylent](https://github.com/soylent). +* [#1745](https://github.com/ruby-grape/grape/pull/1745): Rescue truly all exceptions - [@mtsmfm](https://github.com/mtsmfm). * Your contribution here. ### 1.0.2 (1/10/2018) diff --git a/README.md b/README.md index 14c938b41e..228d79e98a 100644 --- a/README.md +++ b/README.md @@ -2086,7 +2086,7 @@ literally accepts every request. ## Exception Handling -Grape can be told to rescue all `StandardError` exceptions and return them in the API format. +Grape can be told to rescue all exceptions and return them in the API format. ```ruby class Twitter::API < Grape::API @@ -2116,7 +2116,7 @@ class Twitter::API < Grape::API end ``` -In this case ```UserDefinedError``` must be inherited from ```StandardError```. +```UserDefinedError``` can be inherited from any exception class. Notice that you could combine these two approaches (rescuing custom errors takes precedence). For example, it's useful for handling all exceptions except Grape validation errors. diff --git a/lib/grape/middleware/error.rb b/lib/grape/middleware/error.rb index f7216494ca..aae83fbf4e 100644 --- a/lib/grape/middleware/error.rb +++ b/lib/grape/middleware/error.rb @@ -110,7 +110,7 @@ def rescue_handler_for_grape_exception(klass) end def rescue_handler_for_any_class(klass) - return unless klass <= StandardError + return unless klass <= Exception return unless options[:rescue_all] || options[:rescue_grape_exceptions] options[:all_rescue_handler] || :default_rescue_handler diff --git a/spec/grape/middleware/exception_spec.rb b/spec/grape/middleware/exception_spec.rb index 2f65cc5416..e2438f5e83 100644 --- a/spec/grape/middleware/exception_spec.rb +++ b/spec/grape/middleware/exception_spec.rb @@ -103,8 +103,13 @@ def app run ExceptionSpec::OtherExceptionApp end end - it 'does not trap errors other than StandardError' do - expect { get '/' }.to raise_error(NotImplementedError, 'snow!') + it 'sets the message appropriately' do + get '/' + expect(last_response.body).to eq('snow!') + end + it 'defaults to a 500 status' do + get '/' + expect(last_response.status).to eq(500) end end end