Exception: Servus::Support::Errors::GuardError

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

Overview

Guard validation failure with custom code.

Guards define their own error code and HTTP status via the DSL.

Examples:

GuardError.new("Amount must be positive", code: 'invalid_amount', http_status: 422)

Constant Summary collapse

DEFAULT_MESSAGE =
'Guard validation failed'

Instance Attribute Summary collapse

Attributes inherited from ServiceError

#message

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, code: 'guard_failed', http_status: :unprocessable_entity) ⇒ GuardError

Creates a new guard error with metadata.

Parameters:

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

    error message

  • code (String) (defaults to: 'guard_failed')

    error code for API responses (default: 'guard_failed')

  • http_status (Symbol, Integer) (defaults to: :unprocessable_entity)

    HTTP status (default: :unprocessable_entity)



77
78
79
80
81
# File 'lib/servus/support/errors.rb', line 77

def initialize(message = nil, code: 'guard_failed', http_status: :unprocessable_entity)
  super(message)
  @code        = code
  @http_status = http_status
end

Instance Attribute Details

#codeString (readonly)

Returns application-specific error code.

Returns:

  • (String)

    application-specific error code



67
68
69
# File 'lib/servus/support/errors.rb', line 67

def code
  @code
end

#http_statusSymbol, Integer (readonly)

Returns HTTP status code.

Returns:

  • (Symbol, Integer)

    HTTP status code



70
71
72
# File 'lib/servus/support/errors.rb', line 70

def http_status
  @http_status
end

Instance Method Details

#api_errorObject



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

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