Class: Jersey::Middleware::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/jersey/middleware/error_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



3
4
5
6
7
# File 'lib/jersey/middleware/error_handler.rb', line 3

def initialize(app, options = {})
  @app = app
  ib = options[:include_backtrace]
  @include_backtrace = (ib.nil? && ENV['RACK_ENV'] == 'development') || ib
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jersey/middleware/error_handler.rb', line 9

def call(env)
  begin
    @app.call(env)
  rescue => e
    # get status code from Jersey Errors
    if e.class.const_defined?(:STATUS_CODE)
      status = e.class::STATUS_CODE
    elsif e.respond_to?(:status_code)
      status = e.status_code
    else
      status = 500
    end

    headers = {
      'Content-Type' => 'application/json'
    }

    body = {error: {
      type: e.class.name.split('::').last,
      request_id: env['REQUEST_ID'] || env['HTTP_REQUEST_ID'],
      message: e.message
    }}

    if @include_backtrace
      body[:error][:backtrace] = e.backtrace
    end

    [status, headers, [body.to_json]]
  end
end