Module: Apes::Concerns::Errors

Included in:
Apes::Controller
Defined in:
lib/apes/concerns/errors.rb

Overview

Errors handling module.

Constant Summary

ERROR_HANDLERS =

Default map of error handlers

{
  "ActiveRecord::RecordNotFound" => :error_handle_not_found,
  "Apes::Errors::AuthenticationError" => :error_handle_fordidden,
  "Apes::Errors::InvalidModelError" => :error_handle_invalid_source,
  "Apes::Errors::BadRequestError" => :error_handle_bad_request,
  "Apes::Errors::MissingDataError" => :error_handle_missing_data,
  "Apes::Errors::InvalidDataError" => :error_handle_invalid_data,
  "JSON::ParserError" => :error_handle_invalid_data,
  "ActiveRecord::RecordInvalid" => :error_handle_validation,
  "ActiveRecord::UnknownAttributeError" => :error_handle_unknown_attribute,
  "ActionController::UnpermittedParameters" => :error_handle_unknown_attribute,
  "Apes::Errors::BaseError" => :error_handle_general,
  "Lazier::Exceptions::Debug" => :error_handle_debug
}.freeze

Instance Method Summary (collapse)

Instance Method Details

- (Object) error_handle_bad_request(_ = nil)

Handles requests containing invalid data.



81
82
83
84
# 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

- (Object) error_handle_debug(exception)

Handles debug exceptions.

Parameters:

  • exception (Exception)

    The exception to handle.



63
64
65
# 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

- (Object) error_handle_exception(exception)

Default unexpected exception handler.

Parameters:

  • exception (Exception)

    The exception to handle.



38
39
40
41
# 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

- (Object) error_handle_fordidden(exception)

Handles unauthorized requests.

Parameters:

  • exception (Exception)

    The exception to handle.



70
71
72
73
# 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

- (Object) error_handle_general(exception)

Handles base exceptions.

Parameters:

  • exception (Exception)

    The exception to handle.



46
47
48
# File 'lib/apes/concerns/errors.rb', line 46

def error_handle_general(exception)
  render_error(exception.details[:status], exception.details[:error])
end

- (Object) error_handle_invalid_data(_ = nil)

Handles requests that send invalid data.



93
94
95
96
# 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

- (Object) error_handle_missing_data(_ = nil)

Handles requests that miss data.



87
88
89
90
# File 'lib/apes/concerns/errors.rb', line 87

def error_handle_missing_data(_ = nil)
  @reason = "Missing data."
  render("errors/400", status: :bad_request)
end

- (Object) error_handle_not_found(_ = nil)

Handles requests of missing data.



76
77
78
# File 'lib/apes/concerns/errors.rb', line 76

def error_handle_not_found(_ = nil)
  render("errors/404", status: :not_found)
end

- (Object) error_handle_others(exception)

Handles other exceptions.

Parameters:

  • exception (Exception)

    The exception to handle.



53
54
55
56
57
58
# 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

- (Object) error_handle_unknown_attribute(exception)

Handles requests that send data with unexpected attributes.



99
100
101
102
# 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

- (Object) error_handle_validation(exception)

Handles requests that send data with invalid attributes.



105
106
107
108
# 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

- (Object) fail_request!(status, error)

Handles a failed request.

Parameters:

  • status (Symbol|Fixnum)

    The HTTP error code.

  • error (Object)

    The occurred error.

Raises:



31
32
33
# File 'lib/apes/concerns/errors.rb', line 31

def fail_request!(status, error)
  raise(::Apes::Errors::BaseError, {status: status, error: error})
end