Class: Grape::Middleware::Error

Inherits:
Base
  • Object
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

Methods included from DSL::Headers

#header

Methods included from Helpers

#context

Constructor Details

#initialize(app, *options) ⇒ Error

Returns a new instance of Error.



29
30
31
32
# File 'lib/grape/middleware/error.rb', line 29

def initialize(app, *options)
  super
  self.class.send(:include, @options[:helpers]) if @options[:helpers]
end

Instance Method Details

#call!(env) ⇒ Object



34
35
36
37
38
39
# File 'lib/grape/middleware/error.rb', line 34

def call!(env)
  @env = env
  error_response(catch(:error) { return @app.call(@env) })
rescue Exception => e # rubocop:disable Lint/RescueException
  run_rescue_handler(find_handler(e.class), e, @env[Grape::Env::API_ENDPOINT])
end

#default_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/grape/middleware/error.rb', line 8

def default_options
  {
    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