class ParamsParserRescue
  
  def call(env)
    begin
      @app.call(env)
    rescue MultiJson::DecodeError => e
      http_error(400, e.message)
    end

  end

  ##
  # Outputs an HTTP `4xx` or `5xx` response.
  #
  # @param  [Integer]                code
  # @param  [String, #to_s]          message
  # @param  [Hash{String => String}] headers
  # @return [Array(Integer, Hash, #each)]
  def http_error(code, message = nil, headers = {})
    [code,
     {'Content-Type' => 'text/plain; charset=utf-8'}.merge(headers),
     Array(http_status(code) + (message.nil? ? "\n" : " (#{message})\n"))
    ]
  end
end