module Apes::Concerns::Errors

Errors handling module.

Constants

ERROR_HANDLERS

Default map of error handlers

Public Instance Methods

error_handle_bad_request(_ = nil) click to toggle source

Handles requests containing invalid data.

# File lib/apes/concerns/errors.rb, line 81
def error_handle_bad_request(_ = nil)
  @reason = "Invalid Content-Type specified. Please use \"#{request_valid_content_type}\" when performing write operations."
  render("errors/400", status: :bad_request)
end
error_handle_debug(exception) click to toggle source

Handles debug exceptions.

@param exception [Exception] The exception to handle.

# File lib/apes/concerns/errors.rb, line 63
def error_handle_debug(exception)
  render("errors/400", status: 418, locals: {debug: YAML.load(exception.message)})
end
error_handle_exception(exception) click to toggle source

Default unexpected exception handler.

@param exception [Exception] The exception to handle.

# File lib/apes/concerns/errors.rb, line 38
def error_handle_exception(exception)
  handler = ERROR_HANDLERS.fetch(exception.class.to_s, :error_handle_others)
  send(handler, exception)
end
error_handle_fordidden(exception) click to toggle source

Handles unauthorized requests.

@param exception [Exception] The exception to handle.

# File lib/apes/concerns/errors.rb, line 70
def error_handle_fordidden(exception)
  @authentication_error = {error: exception.message.present? ? exception.message : "You don't have access to this resource."}
  render("errors/403", status: :forbidden)
end
error_handle_general(exception) click to toggle source

Handles base exceptions.

@param exception [Exception] The exception to handle.

# File lib/apes/concerns/errors.rb, line 46
def error_handle_general(exception)
  render_error(exception.details[:status], exception.details[:error])
end
error_handle_invalid_data(_ = nil) click to toggle source

Handles requests that send invalid data.

# File lib/apes/concerns/errors.rb, line 93
def error_handle_invalid_data(_ = nil)
  @reason = "Invalid data provided."
  render("errors/400", status: :bad_request)
end
error_handle_missing_data(_ = nil) click to toggle source

Handles requests that miss data.

# File lib/apes/concerns/errors.rb, line 87
def error_handle_missing_data(_ = nil)
  @reason = "Missing data."
  render("errors/400", status: :bad_request)
end
error_handle_not_found(_ = nil) click to toggle source

Handles requests of missing data.

# File lib/apes/concerns/errors.rb, line 76
def error_handle_not_found(_ = nil)
  render("errors/404", status: :not_found)
end
error_handle_others(exception) click to toggle source

Handles other exceptions.

@param exception [Exception] The exception to handle.

# File lib/apes/concerns/errors.rb, line 53
def error_handle_others(exception)
  @exception = exception
  @backtrace = exception.backtrace
     .slice(0, 50).map { |line| line.gsub(Apes::RuntimeConfiguration.rails_root, "$RAILS").gsub(Apes::RuntimeConfiguration.gems_root, "$GEMS") }
  render("errors/500", status: :internal_server_error)
end
error_handle_unknown_attribute(exception) click to toggle source

Handles requests that send data with unexpected attributes.

# File lib/apes/concerns/errors.rb, line 99
def error_handle_unknown_attribute(exception)
  @errors = exception.is_a?(ActionController::UnpermittedParameters) ? exception.params : exception.attribute
  render("errors/422", status: :unprocessable_entity)
end
error_handle_validation(exception) click to toggle source

Handles requests that send data with invalid attributes.

# File lib/apes/concerns/errors.rb, line 105
def error_handle_validation(exception)
  @errors = exception.record.errors.to_hash
  render("errors/422", status: :unprocessable_entity)
end
fail_request!(status, error) click to toggle source

Handles a failed request.

@param status [Symbol|Fixnum] The HTTP error code. @param error [Object] The occurred error.

# File lib/apes/concerns/errors.rb, line 31
def fail_request!(status, error)
  raise(::Apes::Errors::BaseError, {status: status, error: error})
end