Exception: Syntropy::Error

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

Overview

The base Syntropy error class

Direct Known Subclasses

BadRequestError, ProtocolError, ValidationError

Constant Summary collapse

DEFAULT_STATUS =

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

HTTP::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



53
54
55
56
# File 'lib/syntropy/errors.rb', line 53

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



61
62
63
# File 'lib/syntropy/errors.rb', line 61

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



15
16
17
# File 'lib/syntropy/errors.rb', line 15

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)


24
25
26
# File 'lib/syntropy/errors.rb', line 24

def self.log_error?(err)
  http_status(err) != HTTP::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:



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

def self.method_not_allowed(msg = 'Method not allowed') = new(msg, HTTP::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:



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

def self.not_found(msg = 'Not found') = new(msg, HTTP::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:



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

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