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, #initialize, #mime_types, #response

Methods included from DSL::Headers

#header

Constructor Details

This class inherits a constructor from Grape::Middleware::Base

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
41
42
# File 'lib/grape/middleware/error.rb', line 22

def call!(env)
  @env = env

  inject_helpers!

  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 = ->(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_optionsObject



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 status returned on error
    default_message: '',
    format: :txt,
    formatters: {},
    error_formatters: {},
    rescue_all: false, # true to rescue all exceptions
    rescue_subclasses: true, # rescue subclasses of exceptions listed
    rescue_options: { backtrace: false }, # true to display backtrace
    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

#error!(message, status = , headers = {}, backtrace = []) ⇒ Object



85
86
87
88
# File 'lib/grape/middleware/error.rb', line 85

def error!(message, status = options[:default_status], headers = {}, backtrace = [])
  headers = headers.reverse_merge(Grape::Http::Headers::CONTENT_TYPE => content_type)
  rack_response(format_message(message, backtrace), status, headers)
end

#error_response(error = {}) ⇒ Object

TODO: This method is deprecated. Refactor out.



95
96
97
98
99
100
101
102
# File 'lib/grape/middleware/error.rb', line 95

def error_response(error = {})
  status = error[:status] || options[:default_status]
  message = error[:message] || options[:default_message]
  headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }
  headers.merge!(error[:headers]) if error[:headers].is_a?(Hash)
  backtrace = error[:backtrace] || []
  rack_response(format_message(message, backtrace), status, headers)
end

#exec_handler(e, &handler) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/grape/middleware/error.rb', line 64

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/grape/middleware/error.rb', line 44

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]
  with_option = options[:rescue_options][:with]
  if with_option.instance_of?(Symbol)
    if respond_to?(with_option)
      handler ||= self.class.instance_method(with_option).bind(self)
    else
      fail NoMethodError, "undefined method `#{with_option}'"
    end
  end
  handler
end

#format_message(message, backtrace) ⇒ Object



108
109
110
111
112
113
# File 'lib/grape/middleware/error.rb', line 108

def format_message(message, backtrace)
  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." unless formatter
  formatter.call(message, backtrace, options, env)
end

#handle_error(e) ⇒ Object



90
91
92
# File 'lib/grape/middleware/error.rb', line 90

def handle_error(e)
  error_response(message: e.message, backtrace: e.backtrace)
end

#helpers_available?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/grape/middleware/error.rb', line 81

def helpers_available?
  @helpers
end

#inject_helpers!Object



72
73
74
75
76
77
78
79
# File 'lib/grape/middleware/error.rb', line 72

def inject_helpers!
  return if helpers_available?
  endpoint = @env['api.endpoint']
  self.class.instance_eval do
    include endpoint.send(:helpers)
  end if endpoint.is_a?(Grape::Endpoint)
  @helpers = true
end

#rack_response(message, status = , headers = { Grape::Http::Headers::CONTENT_TYPE => content_type }) ⇒ Object



104
105
106
# File 'lib/grape/middleware/error.rb', line 104

def rack_response(message, status = options[:default_status], headers = { Grape::Http::Headers::CONTENT_TYPE => content_type })
  Rack::Response.new([message], status, headers).finish
end

#rescuable?(klass) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/grape/middleware/error.rb', line 59

def rescuable?(klass)
  return false if klass == Grape::Exceptions::InvalidVersionHeader
  options[:rescue_all] || (options[:rescue_handlers] || []).any? { |error, _handler| klass <= error } || (options[:base_only_rescue_handlers] || []).include?(klass)
end