class Onsi::ErrorResponse
The error response container.
@author Maddie Schipper @since 1.0.0
@example
def handle_error(error) response = Onsi::ErrorResponse.new(400) response.add(400, 'bad_request', title: 'The payload was invalid') render_error(response) end
Attributes
status[R]
The HTTP status for the response.
@return [Integer]
Public Class Methods
new(status)
click to toggle source
Create a new ErrorResponse
@param status [Integer] The HTTP status for the response.
# File lib/onsi/error_responder.rb, line 209 def initialize(status) @status = status @errors = [] end
Public Instance Methods
add(status, code, title: nil, details: nil, meta: nil)
click to toggle source
Add a renderable error to the errors
@param status [#to_s, nil] The status of the error. Usually the same as
the HTTP status passed to #initialize
@param code [String] The error code for the error. e.g. `bad_request`
@param title [String, nil] The user displayable title for the error.
@param details [String, nil] The user displayable details for the error.
@param meta [Hash, nil] Any additional metadata to associate
with the error.
# File lib/onsi/error_responder.rb, line 228 def add(status, code, title: nil, details: nil, meta: nil) @errors << {}.tap do |err| err[:status] = (status || @status).to_s err[:code] = code err[:title] = title if title.present? err[:detail] = details if details.present? err[:meta] = Hash(meta) if meta.present? end end
as_json()
click to toggle source
Create the error objects.
@return [Hash] The JSON-API error hash.
# File lib/onsi/error_responder.rb, line 242 def as_json { errors: @errors.as_json } end
renderable()
click to toggle source
Returns a hash that can be passed to render
@private
# File lib/onsi/error_responder.rb, line 250 def renderable { json: as_json, status: status } end