-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate response from the exception handler. Closes #1757.
- Loading branch information
1 parent
292976d
commit c117bff
Showing
10 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2183,7 +2183,7 @@ You can also rescue all exceptions with a code block and handle the Rack respons | |
```ruby | ||
class Twitter::API < Grape::API | ||
rescue_from :all do |e| | ||
Rack::Response.new([ e.message ], 500, { 'Content-type' => 'text/error' }).finish | ||
Rack::Response.new([ e.message ], 500, { 'Content-type' => 'text/error' }) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ghiculescu
Contributor
|
||
end | ||
end | ||
``` | ||
|
@@ -2254,9 +2254,9 @@ class Twitter::API < Grape::API | |
end | ||
``` | ||
|
||
The `rescue_from` block must return a `Rack::Response` object, call `error!` or re-raise an exception. | ||
The `rescue_from` handler must return a `Rack::Response` object, call `error!`, or raise an exception (either the original exception or another custom one). The exception raised in `rescue_from` will be handled outside Grape. For example, if you mount Grape in Rails, the exception will be handle by [Rails Action Controller](https://guides.rubyonrails.org/action_controller_overview.html#rescue). | ||
|
||
The `with` keyword is available as `rescue_from` options, it can be passed method name or Proc object. | ||
Alternately, use the `with` option in `rescue_from` to specify a method or a `proc`. | ||
|
||
```ruby | ||
class Twitter::API < Grape::API | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Grape | ||
module Exceptions | ||
class InvalidResponse < Base | ||
def initialize | ||
super(message: compose_message(:invalid_response)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'spec_helper' | ||
|
||
describe Grape::Exceptions::InvalidResponse do | ||
describe '#message' do | ||
let(:error) { described_class.new } | ||
|
||
it 'contains the problem in the message' do | ||
expect(error.message).to include('Invalid response') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i feel like this change should be documented better. the fact you need to remove
.finish
from your response is not very clear in the PR body or changelog, so it resulted in quite a confusing debugging session when upgrading to this version of grape.