Class: Hyperdrive::Middleware::Error

Inherits:
Object
  • Object
show all
Includes:
Values
Defined in:
lib/hyperdrive/middleware/error.rb

Instance Method Summary collapse

Methods included from Values

default_config, default_cors_options, default_headers, definable_request_methods, http_request_methods, request_methods, supported_request_methods

Constructor Details

#initialize(app) ⇒ Error

Returns a new instance of Error.



8
9
10
# File 'lib/hyperdrive/middleware/error.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/hyperdrive/middleware/error.rb', line 12

def call(env)
  @app.call(env)
rescue => e
  error_message = "#{e.class}: #{e.message}"
  $stdout.sync = true
  $stdout.puts error_message
  headers = { 'Content-Type' => 'application/json' }
  if e.respond_to?(:http_status_code)
    status = e.http_status_code
    body = [json_error(e)]
  else
    status = 500
    error = if ENV['RACK_ENV'] == 'production'
              Hyperdrive::Errors::UnknownError.new
            else
              Hyperdrive::Errors::UnknownError.new(error_message)
            end
    body = [json_error(error)]
  end
  [status, headers, body]
end