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)



129
130
131
132
133
# File 'lib/servus/support/errors.rb', line 129

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



119
120
121
# File 'lib/servus/support/errors.rb', line 119

def code
  @code
end

#http_statusSymbol, Integer (readonly)

Returns HTTP status code.

Returns:

  • (Symbol, Integer)

    HTTP status code



122
123
124
# File 'lib/servus/support/errors.rb', line 122

def http_status
  @http_status
end

Instance Method Details

#api_errorObject



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

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