Class: Grape::Middleware::Error
- Inherits:
-
Base
- Object
- Base
- Grape::Middleware::Error
show all
- Defined in:
- lib/grape/middleware/error.rb
Constant Summary
Constants inherited
from Base
Base::TEXT_HTML
Instance Attribute Summary
Attributes inherited from Base
#app, #env, #options
Instance Method Summary
collapse
-
#call!(env) ⇒ Object
-
#default_options ⇒ Object
-
#error!(message, status = , headers = {}, backtrace = [], original_exception = nil) ⇒ Object
-
#error_response(error = {}) ⇒ Object
TODO: This method is deprecated.
-
#exec_handler(e, &handler) ⇒ Object
-
#find_handler(klass) ⇒ Object
-
#format_message(message, backtrace, original_exception = nil) ⇒ Object
-
#handle_error(e) ⇒ Object
-
#initialize(app, **options) ⇒ Error
constructor
-
#rack_response(message, status = , headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) ⇒ Object
-
#rescuable?(klass) ⇒ Boolean
-
#rescuable_by_grape?(klass) ⇒ Boolean
Methods inherited from Base
#after, #before, #call, #content_type, #content_type_for, #content_types, #mime_types, #response
#header
Constructor Details
#initialize(app, **options) ⇒ Error
Returns a new instance of Error.
27
28
29
30
|
# File 'lib/grape/middleware/error.rb', line 27
def initialize(app, **options)
super
self.class.send(:include, @options[:helpers]) if @options[:helpers]
end
|
Instance Method Details
#call!(env) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/grape/middleware/error.rb', line 32
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 || rescuable_by_grape?(e.class))
handler = ->(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
21
22
23
24
25
|
# File 'lib/grape/middleware/error.rb', line 6
def default_options
{
default_status: 500,
default_message: '',
format: :txt,
helpers: nil,
formatters: {},
error_formatters: {},
rescue_all: false,
rescue_grape_exceptions: false,
rescue_subclasses: true,
rescue_options: {
backtrace: false,
original_exception: false,
},
rescue_handlers: {},
base_only_rescue_handlers: {},
all_rescue_handler: nil
}
end
|
#error!(message, status = , headers = {}, backtrace = [], original_exception = nil) ⇒ Object
83
84
85
86
|
# File 'lib/grape/middleware/error.rb', line 83
def error!(message, status = options[:default_status], = {}, backtrace = [], original_exception = nil)
= .reverse_merge(Grape::Http::::CONTENT_TYPE => content_type)
rack_response(format_message(message, backtrace, original_exception), status, )
end
|
#error_response(error = {}) ⇒ Object
TODO: This method is deprecated. Refactor out.
93
94
95
96
97
98
99
100
101
|
# File 'lib/grape/middleware/error.rb', line 93
def error_response(error = {})
status = error[:status] || options[:default_status]
message = error[:message] || options[:default_message]
= { Grape::Http::::CONTENT_TYPE => content_type }
.merge!(error[:headers]) if error[:headers].is_a?(Hash)
backtrace = error[:backtrace] || error[:original_exception] && error[:original_exception].backtrace || []
original_exception = error.is_a?(Exception) ? error : error[:original_exception] || nil
rack_response(format_message(message, backtrace, original_exception), status, )
end
|
#exec_handler(e, &handler) ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/grape/middleware/error.rb', line 75
def exec_handler(e, &handler)
if handler.lambda? && handler.arity.zero?
instance_exec(&handler)
else
instance_exec(e, &handler)
end
end
|
#find_handler(klass) ⇒ Object
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/grape/middleware/error.rb', line 52
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]
if handler.instance_of?(Symbol)
raise NoMethodError, "undefined method `#{handler}'" unless respond_to?(handler)
handler = self.class.instance_method(handler).bind(self)
end
handler
end
|
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/grape/middleware/error.rb', line 107
def format_message(message, backtrace, original_exception = nil)
format = env[Grape::Env::API_FORMAT] || options[:format]
formatter = Grape::ErrorFormatter.formatter_for(format, options)
throw :error,
status: 406,
message: "The requested format '#{format}' is not supported.",
backtrace: backtrace,
original_exception: original_exception unless formatter
formatter.call(message, backtrace, options, env, original_exception)
end
|
#handle_error(e) ⇒ Object
88
89
90
|
# File 'lib/grape/middleware/error.rb', line 88
def handle_error(e)
error_response(message: e.message, backtrace: e.backtrace, original_exception: e)
end
|
#rack_response(message, status = , headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) ⇒ Object
103
104
105
|
# File 'lib/grape/middleware/error.rb', line 103
def rack_response(message, status = options[:default_status], = { Grape::Http::::CONTENT_TYPE => content_type })
Rack::Response.new([message], status, ).finish
end
|
#rescuable?(klass) ⇒ Boolean
65
66
67
68
|
# File 'lib/grape/middleware/error.rb', line 65
def rescuable?(klass)
return false if klass == Grape::Exceptions::
rescue_all? || rescue_class_or_its_ancestor?(klass) || rescue_with_base_only_handler?(klass)
end
|
#rescuable_by_grape?(klass) ⇒ Boolean
70
71
72
73
|
# File 'lib/grape/middleware/error.rb', line 70
def rescuable_by_grape?(klass)
return false if klass == Grape::Exceptions::
options[:rescue_grape_exceptions]
end
|