Module: BetterService::ErrorCodes

Defined in:
lib/better_service/errors/better_service_error.rb

Overview

Standard error codes used in BetterService responses

These codes are used for both hash responses and exception codes, providing consistent error identification across the system.

Examples:

Using error codes in exceptions

raise Errors::Runtime::ValidationError.new(
  "Validation failed",
  code: ErrorCodes::VALIDATION_FAILED,
  context: { validation_errors: {...} }
)

Handling errors by code

begin
  service.call
rescue BetterServiceError => e
  case e.code
  when ErrorCodes::VALIDATION_FAILED
    render json: { errors: e.context[:validation_errors] }, status: :unprocessable_entity
  when ErrorCodes::UNAUTHORIZED
    render json: { error: e.message }, status: :forbidden
  end
end

Constant Summary collapse

VALIDATION_FAILED =

Validation failed - input parameters are invalid

Used when Dry::Schema validation fails. The error will include validation details in context.

Examples:

raise Errors::Runtime::ValidationError.new(
  "Validation failed",
  code: :validation_failed,
  context: {
    validation_errors: { email: ["is invalid"], age: ["must be greater than 18"] }
  }
)
:validation_failed
UNAUTHORIZED =

Authorization failed - user doesn’t have permission

Used when the authorize_with block returns false.

Examples:

raise Errors::Runtime::AuthorizationError.new(
  "Not authorized to perform this action",
  code: :unauthorized,
  context: { user_id: 123, action: "destroy" }
)
:unauthorized
SCHEMA_REQUIRED =

Schema is missing or invalid

:schema_required
CONFIGURATION_ERROR =

Service configuration is invalid

:configuration_error
EXECUTION_ERROR =

Unexpected error during service execution

:execution_error
RESOURCE_NOT_FOUND =

Required resource was not found

:resource_not_found
TRANSACTION_ERROR =

Database transaction failed

:transaction_error
DATABASE_ERROR =

Database operation failed (validation errors, save failures)

:database_error
WORKFLOW_FAILED =

Workflow execution failed

:workflow_failed
STEP_FAILED =

Workflow step failed

:step_failed
ROLLBACK_FAILED =

Workflow rollback failed

:rollback_failed
INVALID_RESULT =

Service returned invalid result type (not BetterService::Result)

:invalid_result