Class: Grape::Middleware::Error
- Defined in:
- lib/grape/middleware/error.rb
Constant Summary
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #call!(env) ⇒ Object
- #default_options ⇒ Object
- #default_rescue_handler(e) ⇒ Object
- #error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil) ⇒ Object
-
#error_response(error = {}) ⇒ Object
TODO: This method is deprecated.
- #format_message(message, backtrace, original_exception = nil) ⇒ Object
-
#initialize(app, **options) ⇒ Error
constructor
A new instance of Error.
- #rack_response(message, status = , headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) ⇒ Object
Methods inherited from Base
#after, #before, #call, #content_type, #content_type_for, #content_types, #mime_types, #response
Methods included from DSL::Headers
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, **) 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 |
# 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 Exception => error # rubocop:disable Lint/RescueException 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
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_status: 500, # default status returned on error default_message: '', format: :txt, helpers: nil, formatters: {}, error_formatters: {}, rescue_all: false, # true to rescue all exceptions rescue_grape_exceptions: false, rescue_subclasses: true, # rescue subclasses of exceptions listed rescue_options: { backtrace: false, # true to display backtrace, true to let Grape handle Grape::Exceptions original_exception: false, # true to display exception }, rescue_handlers: {}, # rescue handler blocks base_only_rescue_handlers: {}, # rescue handler blocks rescuing only the base class all_rescue_handler: nil # rescue handler block to rescue from all exceptions } end |
#default_rescue_handler(e) ⇒ Object
56 57 58 |
# File 'lib/grape/middleware/error.rb', line 56 def default_rescue_handler(e) error_response(message: e., backtrace: e.backtrace, original_exception: e) end |
#error!(message, status = options[:default_status], headers = {}, backtrace = [], original_exception = nil) ⇒ Object
51 52 53 54 |
# File 'lib/grape/middleware/error.rb', line 51 def error!(, status = [:default_status], headers = {}, backtrace = [], original_exception = nil) headers = headers.reverse_merge(Grape::Http::Headers::CONTENT_TYPE => content_type) rack_response((, backtrace, original_exception), status, headers) end |
#error_response(error = {}) ⇒ Object
TODO: This method is deprecated. Refactor out.
61 62 63 64 65 66 67 68 69 |
# File 'lib/grape/middleware/error.rb', line 61 def error_response(error = {}) status = error[:status] || [:default_status] = error[:message] || [:default_message] headers = { Grape::Http::Headers::CONTENT_TYPE => content_type } headers.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((, backtrace, original_exception), status, headers) end |
#format_message(message, backtrace, original_exception = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/grape/middleware/error.rb', line 75 def (, backtrace, original_exception = nil) format = env[Grape::Env::API_FORMAT] || [:format] formatter = Grape::ErrorFormatter.formatter_for(format, ) throw :error, status: 406, message: "The requested format '#{format}' is not supported.", backtrace: backtrace, original_exception: original_exception unless formatter formatter.call(, backtrace, , env, original_exception) end |
#rack_response(message, status = , headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) ⇒ Object
71 72 73 |
# File 'lib/grape/middleware/error.rb', line 71 def rack_response(, status = [:default_status], headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) Rack::Response.new([], status, headers).finish end |