Exception: Syntropy::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/syntropy/errors.rb

Overview

The base Syntropy error class

Direct Known Subclasses

ValidationError

Constant Summary collapse

Status =
Qeweney::Status
DEFAULT_STATUS =

By default, the HTTP status for errors is 500 Internal Server Error.

Status::INTERNAL_SERVER_ERROR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg = 'Internal server error', http_status = DEFAULT_STATUS) ⇒ void

Initializes a Syntropy error with the given HTTP status and message.

Parameters:

  • http_status (Integer, String) (defaults to: DEFAULT_STATUS)

    HTTP status

  • msg (String) (defaults to: 'Internal server error')

    error message



55
56
57
58
# File 'lib/syntropy/errors.rb', line 55

def initialize(msg = 'Internal server error', http_status = DEFAULT_STATUS)
  super(msg)
  @http_status = http_status
end

Instance Attribute Details

#http_statusInteger, String (readonly)

Returns the HTTP status for the error.

Returns:

  • (Integer, String)

    HTTP status



63
64
65
# File 'lib/syntropy/errors.rb', line 63

def http_status
  @http_status
end

Class Method Details

.http_status(err) ⇒ Integer, String

Returns the HTTP status for the given exception.

Parameters:

  • err (Exception)

    exception

Returns:

  • (Integer, String)

    HTTP status



17
18
19
# File 'lib/syntropy/errors.rb', line 17

def self.http_status(err)
  err.respond_to?(:http_status) ? err.http_status : DEFAULT_STATUS
end

.log_error?(err) ⇒ bool

Returns true if the error should be logged. Currently all errors are logged except for NOT FOUND errors.

Parameters:

  • err (Exception)

    error

Returns:

  • (bool)


26
27
28
# File 'lib/syntropy/errors.rb', line 26

def self.log_error?(err)
  http_status(err) != Status::NOT_FOUND
end

.method_not_allowed(msg = 'Method not allowed') ⇒ Syntropy::Error

Creates an error with status 405 Method Not Allowed.

Parameters:

  • msg (String) (defaults to: 'Method not allowed')

    error message

Returns:



40
# File 'lib/syntropy/errors.rb', line 40

def self.method_not_allowed(msg = 'Method not allowed') = new(msg, Status::METHOD_NOT_ALLOWED)

.not_found(msg = 'Not found') ⇒ Syntropy::Error

Creates an error with status 404 Not Found.

Parameters:

  • msg (String) (defaults to: 'Not found')

    error message

Returns:



34
# File 'lib/syntropy/errors.rb', line 34

def self.not_found(msg = 'Not found') = new(msg, Status::NOT_FOUND)

.teapot(msg = 'I\'m a teapot') ⇒ Syntropy::Error

Creates an error with status 418 I’m a teapot.

Parameters:

  • msg (String) (defaults to: 'I\'m a teapot')

    error message

Returns:



46
# File 'lib/syntropy/errors.rb', line 46

def self.teapot(msg = 'I\'m a teapot') = new(msg, Status::TEAPOT)