-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
error_handler.rb
40 lines (35 loc) · 996 Bytes
/
error_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require_relative 'constants'
module Rack
class ErrorHandler
def initialize(app)
@app = app
end
def log_error(env, error)
if error_stream = env[RACK_ERRORS]
# Exception#full_message was introduced in Ruby 2.5+. If it's not
# available, we fall back to the old behavior of just printing the
# exception message.
if error.respond_to?(:full_message)
error_stream.puts(error.full_message)
else
error_stream.puts(error.message)
end
end
end
def bad_request(error)
[400, {}, ["Bad Request: #{error.class}"]]
end
def internal_server_error(error)
[500, {}, ["Internal Server Error: #{error.class}"]]
end
def call(env)
@app.call(env)
rescue BadRequest => error
log_error(env, error)
return bad_request(error)
rescue ScriptError, StandardError => error
log_error(env, error)
return internal_server_error(error)
end
end
end