Class: Gris::Middleware::ErrorHandlers

Inherits:
Object
  • Object
show all
Defined in:
lib/gris/middleware/error_handlers.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ ErrorHandlers

Returns a new instance of ErrorHandlers.



4
5
6
# File 'lib/gris/middleware/error_handlers.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  response = @app.call env
  case response[0]
  when 400..500
    format_error_response response
  else
    response
  end
rescue RuntimeError => e
  error = { status: 500, message: e.message }
  error_response error.to_json, 500
rescue ::ActiveRecord::RecordNotFound => e
  error = { status: 404, message: e.message }
  error_response error.to_json, 404
rescue ::ActiveRecord::RecordInvalid => e
  error = { status: 409, message: e.message }
  error_response error.to_json, 409
end