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
Methods inherited from Base
#after, #before, #call, #content_type, #content_type_for, #content_types, #mime_types, #response
#header
Constructor Details
#initialize(app, **options) ⇒ Error
28
29
30
31
|
# File 'lib/grape/middleware/error.rb', line 28
def initialize(app, **options)
super
self.class.send(:include, @options[:helpers]) if @options[:helpers]
end
|
Instance Method Details
#call!(env) ⇒ Object
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 33
def call!(env)
@env = env
begin
error_response(catch(:error) do
return @app.call(@env)
end)
rescue Exception => error
handler =
rescue_handler_for_base_only_class(error.class) ||
rescue_handler_for_class_or_its_ancestor(error.class) ||
rescue_handler_for_grape_exception(error.class) ||
rescue_handler_for_any_class(error.class) ||
raise
run_rescue_handler(handler, error)
end
end
|
#default_options ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/grape/middleware/error.rb', line 7
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
|
#default_rescue_handler(e) ⇒ Object
57
58
59
|
# File 'lib/grape/middleware/error.rb', line 57
def default_rescue_handler(e)
error_response(message: e.message, backtrace: e.backtrace, original_exception: e)
end
|
#error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil) ⇒ Object
52
53
54
55
|
# File 'lib/grape/middleware/error.rb', line 52
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.
62
63
64
65
66
67
68
69
70
|
# File 'lib/grape/middleware/error.rb', line 62
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
|
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/grape/middleware/error.rb', line 79
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
|
#rack_response(message, status = , headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) ⇒ Object
72
73
74
75
76
77
|
# File 'lib/grape/middleware/error.rb', line 72
def rack_response(message, status = options[:default_status], = { Grape::Http::::CONTENT_TYPE => content_type })
if [Grape::Http::::CONTENT_TYPE] == TEXT_HTML
message = ERB::Util.html_escape(message)
end
Rack::Response.new([message], status, )
end
|