Class: Rack::Middleware::JsonErrorMsg

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/middleware/json-error-msg.rb

Defined Under Namespace

Classes: Error, Railtie

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ JsonErrorMsg

Returns a new instance of JsonErrorMsg.



17
18
19
# File 'lib/rack/middleware/json-error-msg.rb', line 17

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



15
16
17
# File 'lib/rack/middleware/json-error-msg.rb', line 15

def app
  @app
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/middleware/json-error-msg.rb', line 21

def call(env)
  begin
    @app.call(env)
  rescue ActionDispatch::ParamsParser::ParseError => error
    if env['HTTP_ACCEPT'] =~ /json/
      error_output = "There was a problem in the request you submitted: #{error}"
      return [
        400, { 'Content-Type' => 'application/json' },
        [ { request_id: env['action_dispatch.request_id'], error: error_output }.to_json ]
      ]
    else
      raise error
    end
  end
end