Exception: Servus::Support::Errors::ServiceError

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

Overview

Base error class for all Servus service errors.

Subclasses define their HTTP status via #http_status and their API response format via #api_error.

Examples:

Creating a custom error type

class InsufficientFundsError < Servus::Support::Errors::ServiceError
  DEFAULT_MESSAGE = 'Insufficient funds'

  def http_status = :unprocessable_entity

  def api_error
    { code: 'insufficient_funds', message: message }
  end
end

Using with failure method

def call
  return failure("User not found", type: NotFoundError)
end

Constant Summary collapse

DEFAULT_MESSAGE =
'An error occurred'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil) ⇒ ServiceError

Creates a new service error instance.

Parameters:

  • message (String, nil) (defaults to: nil)

    custom error message (uses DEFAULT_MESSAGE if nil)



41
42
43
44
# File 'lib/servus/support/errors.rb', line 41

def initialize(message = nil)
  @message = message || self.class::DEFAULT_MESSAGE
  super("#{self.class}: #{@message}")
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



34
35
36
# File 'lib/servus/support/errors.rb', line 34

def message
  @message
end

Instance Method Details

#api_errorHash

Returns an API-friendly error response.

Returns:

  • (Hash)

    hash with :code and :message keys



54
# File 'lib/servus/support/errors.rb', line 54

def api_error = { code: http_status, message: message }

#http_statusSymbol

Returns the HTTP status code for this error.

Returns:

  • (Symbol)

    Rails-compatible status symbol



49
# File 'lib/servus/support/errors.rb', line 49

def http_status = :bad_request