Class: Grape::Middleware::Error
- Inherits:
-
Base
- Object
- Base
- Grape::Middleware::Error
show all
- Defined in:
- lib/grape/middleware/error.rb
Instance Attribute Summary
Attributes inherited from Base
#app, #env, #options
Instance Method Summary
collapse
Methods inherited from Base
#after, #before, #call, #content_type, #content_type_for, #content_types, #initialize, #mime_types, #response
Instance Method Details
#call!(env) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/grape/middleware/error.rb', line 22
def call!(env)
@env = env
begin
error_response(catch(:error) do
return @app.call(@env)
end)
rescue StandardError => e
is_rescuable = rescuable?(e.class)
if e.is_a?(Grape::Exceptions::Base) && !is_rescuable
handler = lambda { |arg| error_response(arg) }
else
raise unless is_rescuable
handler = find_handler(e.class)
end
handler.nil? ? handle_error(e) : exec_handler(e, &handler)
end
end
|
#default_options ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/grape/middleware/error.rb', line 6
def default_options
{
default_status: 500, default_message: "",
format: :txt,
formatters: {},
error_formatters: {},
rescue_all: false, rescue_subclasses: true, rescue_options: { backtrace: false }, rescue_handlers: {}, base_only_rescue_handlers: {}, all_rescue_handler: nil }
end
|
#error_response(error = {}) ⇒ Object
65
66
67
68
69
70
71
72
|
# File 'lib/grape/middleware/error.rb', line 65
def error_response(error = {})
status = error[:status] || options[:default_status]
message = error[:message] || options[:default_message]
= { 'Content-Type' => content_type }
.merge!(error[:headers]) if error[:headers].is_a?(Hash)
backtrace = error[:backtrace] || []
rack_response(format_message(message, backtrace), status, )
end
|
#exec_handler(e, &handler) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/grape/middleware/error.rb', line 53
def exec_handler(e, &handler)
if handler.lambda? && handler.arity == 0
instance_exec(&handler)
else
instance_exec(e, &handler)
end
end
|
#find_handler(klass) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/grape/middleware/error.rb', line 42
def find_handler(klass)
handler = options[:rescue_handlers].find(-> { [] }) { |error, _| klass <= error }[1]
handler ||= options[:base_only_rescue_handlers][klass]
handler ||= options[:all_rescue_handler]
handler
end
|
78
79
80
81
82
83
|
# File 'lib/grape/middleware/error.rb', line 78
def format_message(message, backtrace)
format = env['api.format'] || options[:format]
formatter = Grape::ErrorFormatter::Base.formatter_for(format, options)
throw :error, status: 406, message: "The requested format '#{format}' is not supported." unless formatter
formatter.call(message, backtrace, options, env)
end
|
#handle_error(e) ⇒ Object
61
62
63
|
# File 'lib/grape/middleware/error.rb', line 61
def handle_error(e)
error_response(message: e.message, backtrace: e.backtrace)
end
|
#rack_response(message, status = , headers = { 'Content-Type' => content_type }) ⇒ Object
74
75
76
|
# File 'lib/grape/middleware/error.rb', line 74
def rack_response(message, status = options[:default_status], = { 'Content-Type' => content_type })
Rack::Response.new([message], status, ).finish
end
|
#rescuable?(klass) ⇒ Boolean
49
50
51
|
# File 'lib/grape/middleware/error.rb', line 49
def rescuable?(klass)
options[:rescue_all] || (options[:rescue_handlers] || []).any? { |error, handler| klass <= error } || (options[:base_only_rescue_handlers] || []).include?(klass)
end
|