Class: GtfsEngine::Middleware::JsonParseErrors

Inherits:
Base
  • Object
show all
Defined in:
lib/gtfs_engine/middleware/json_parse_errors.rb

Overview

Normally one handles exceptions with ‘rescue_from`; however, the body of the request is parsed before your controller gets involved, so we need to handle parse errors at the middleware-level.

Rails has a built-in JSON response when such an error occurs, but this middleware will return an error message in JSend format.

Constant Summary collapse

ERROR_CODE =
1001
MESSAGE =
'The body of your request is not valid JSON'

Constants inherited from Base

Base::HEADERS

Instance Attribute Summary

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from GtfsEngine::Middleware::Base

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/gtfs_engine/middleware/json_parse_errors.rb', line 20

def call(env)
  app.call(env)
rescue ActionDispatch::ParamsParser::ParseError => e
  raise e unless accepts_json?(env)

  [400, HEADERS, [create_json(e)]]
end