class BadJsonRequestHandler::Middleware

Public Class Methods

new(app) click to toggle source
# File lib/bad_json_request_handler.rb, line 12
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/bad_json_request_handler.rb, line 16
def call(env)
  begin
    @app.call(env)
  rescue ActionDispatch::Http::Parameters::ParseError => error
    error_output = "Invalid request payload: #{error}"

    if env['CONTENT_TYPE'] =~ /application\/json/
      return [
        400,
        { "Content-Type" => "application/json" },
        [ { errors: { message: error_output } }.to_json ]
      ]
    else
      raise error, error_output
    end
  end
end