Class: Grape::Middleware::Error

Inherits:
Base
  • Object
show all
Defined in:
lib/grape/middleware/error.rb

Constant Summary

Constants inherited from Base

Base::CONTENT_TYPES

Instance Attribute Summary

Attributes inherited from Base

#app, #env, #options

Instance Method Summary collapse

Methods inherited from Base

#after, #before, #call, #content_type, #content_types, #initialize, #mime_types, #request, #response

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

def call!(env)
  @env = env

  begin
    error_response(catch(:error){
      return @app.call(@env)
    })
  rescue Exception => e
    is_rescuable = rescuable?(e.class)
    if e.is_a?(Grape::Exceptions::Base) && !is_rescuable
      handler = lambda {|e| error_response(e) }
    else
      raise unless is_rescuable
      handler = options[:rescue_handlers][e.class] || options[:rescue_handlers][:all]
    end
    handler.nil? ? handle_error(e) : self.instance_exec(e, &handler)
  end
end

#default_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/grape/middleware/error.rb', line 8

def default_options
{
  :default_status => 403, # default status returned on error
  :default_message => "",
  :format => :txt,
  :formatters => {},
  :error_formatters => {},
  :rescue_all => false, # true to rescue all exceptions
  :rescue_options => { :backtrace => false }, # true to display backtrace
  :rescue_handlers => {}, # rescue handler blocks
  :rescued_errors => []
}
end

#error_response(error = {}) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/grape/middleware/error.rb', line 49

def error_response(error = {})
  status = error[:status] || options[:default_status]
  message = error[:message] || options[:default_message]
  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

#format_message(message, backtrace) ⇒ Object



62
63
64
65
66
67
# File 'lib/grape/middleware/error.rb', line 62

def format_message(message, backtrace)
  format = env['api.format'] || options[:format]
  formatter = Grape::ErrorFormatter::Base.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



45
46
47
# File 'lib/grape/middleware/error.rb', line 45

def handle_error(e)
  error_response({ :message => e.message, :backtrace => e.backtrace })
end

#rack_response(message, status = , headers = { 'Content-Type' => content_type }) ⇒ Object



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

def rack_response(message, status = options[:default_status], headers = { 'Content-Type' => content_type })
  Rack::Response.new([ message ], status, headers).finish
end

#rescuable?(klass) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/grape/middleware/error.rb', line 41

def rescuable?(klass)
  options[:rescue_all] || (options[:rescued_errors] || []).include?(klass)
end