Skip to content

Should error! and error_response have the same method signature? #889

Closed
@sunnyrjuneja

Description

@sunnyrjuneja

I was trying to use error! inside of a rescue_from block and got a NoMethodError. I later found error_response and noticed that you don't get the same level of customization that you do from error!.

I'm proposing that error_response and error! have the same method signature. Their current implementations are as follows:

def error_response(error = {})
  status = error[:status] || options[:default_status]
  message = error[:message] || options[:default_message]
  headers = { 'Content-Type' => content_type }
  headers.merge!(error[:headers]) if error[:headers].is_a?(Hash)
  backtrace = error[:backtrace] || []
  rack_response(format_message(message, backtrace), status, headers)
end

def error!(message, status = nil, headers = nil)
  self.status(status || namespace_inheritable(:default_error_status))
  throw :error, message: message, status: self.status, headers: headers
end

My use case was to use error_response like I'm using error! below:

error!({id: 'not_found', message: 'Not Found.'}, 404)

Instead, I'm skipping using error_response and returning a Rack::Response object like this:

Rack::Response.new({
  id: 'interal_error',
  message: 'Interal server error. Check logs.' 
}.to_json, 500, { 'Content-type' => 'text/error' }).finish

Apologies if I'm misunderstanding the API and there's a simple way to do the same thing.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions