Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prefer
Rack::ErrorHandler
middleware.
- Loading branch information
Showing
2 changed files
with
25 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,4 @@ | ||
module Rack | ||
module BadRequest | ||
class Middleware | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def call(env) | ||
@app.call(env) | ||
rescue BadRequest => error | ||
[400, {}, ["Bad Request: #{error.message}"]] | ||
end | ||
end | ||
end | ||
module BadRequest | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Rack | ||
class ErrorHandler | ||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def bad_request(error) | ||
[400, {}, ["Bad Request: #{error.message}"]] | ||
end | ||
|
||
def internal_server_error(error) | ||
[500, {}, ["Internal Server Error: #{error.message}"]] | ||
end | ||
|
||
def call(env) | ||
@app.call(env) | ||
rescue BadRequest => error | ||
return bad_request(error) | ||
rescue => error | ||
return internal_server_error(error) | ||
end | ||
end | ||
end |