apierror

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapErrorToHTTPStatus

func MapErrorToHTTPStatus(err error) int

MapErrorToHTTPStatus maps APIError codes to appropriate HTTP status codes. It unwraps the error chain to find an APIError (value or pointer) and resolves the status from the code catalog, normalizing legacy codes.

func StatusForCode

func StatusForCode(code ErrorCode) int

StatusForCode returns the default HTTP status for an error code. Unknown codes default to 500.

Types

type APIError

type APIError struct {
	Code    ErrorCode   `json:"code"`              // The specific error code that identifies the type of error.
	Message string      `json:"message"`           // A human-readable message that describes the error.
	Details interface{} `json:"details,omitempty"` // Optional field for additional details or context about the error.
}

APIError represents a custom error structure for the API. It includes an error code, message, and optional details to provide additional context for the error.

func NewAPIError

func NewAPIError(code ErrorCode, message string, details interface{}) APIError

NewAPIError creates a new APIError instance. It logs the error details and returns the error object with the provided code, message, and additional details.

func (APIError) Error

func (e APIError) Error() string

Error implements the error interface for APIError. It returns a formatted string combining the error code and message.

type ErrorCode

type ErrorCode string

ErrorCode defines a string type to represent specific error codes used in the API.

const (
	ErrNotFound       ErrorCode = "NOT_FOUND"             // Used when a requested resource is not found.
	ErrConflict       ErrorCode = "CONFLICT"              // Used when a request conflicts with the current state of the resource.
	ErrBadRequest     ErrorCode = "BAD_REQUEST"           // Used when a request contains invalid data or parameters.
	ErrInvalidInput   ErrorCode = "INVALID_INPUT"         // Used when the provided input does not meet the expected format or constraints.
	ErrInternalServer ErrorCode = "INTERNAL_SERVER_ERROR" // Used for general server errors that are not client-related.
	ErrRateLimited    ErrorCode = "RATE_LIMITED"          // Used when a request is rate limited.
)

Predefined error codes to represent different error conditions.

const (
	// GEN — generic / cross-cutting
	ErrGenMalformedRequest ErrorCode = "GEN_MALFORMED_REQUEST"
	ErrGenValidation       ErrorCode = "GEN_VALIDATION_ERROR"
	ErrGenMissingParameter ErrorCode = "GEN_MISSING_PARAMETER"
	ErrGenBadRequest       ErrorCode = "GEN_BAD_REQUEST"
	ErrGenNotFound         ErrorCode = "GEN_NOT_FOUND"
	ErrGenConflict         ErrorCode = "GEN_CONFLICT"
	ErrGenResourceLocked   ErrorCode = "GEN_RESOURCE_LOCKED"
	ErrGenPayloadTooLarge  ErrorCode = "GEN_PAYLOAD_TOO_LARGE"
	ErrGenRateLimited      ErrorCode = "GEN_RATE_LIMITED"
	ErrGenInternal         ErrorCode = "GEN_INTERNAL"

	// AUTH — authentication / authorization
	ErrAuthMissingAPIKey           ErrorCode = "AUTH_MISSING_API_KEY"
	ErrAuthInvalidAPIKey           ErrorCode = "AUTH_INVALID_API_KEY"
	ErrAuthExpiredAPIKey           ErrorCode = "AUTH_EXPIRED_API_KEY"
	ErrAuthMissingPrincipal        ErrorCode = "AUTH_MISSING_PRINCIPAL"
	ErrAuthInsufficientPermissions ErrorCode = "AUTH_INSUFFICIENT_PERMISSIONS"
	ErrAuthUnknownResource         ErrorCode = "AUTH_UNKNOWN_RESOURCE"
	ErrAuthMasterKeyRequired       ErrorCode = "AUTH_MASTER_KEY_REQUIRED"
	ErrAuthCrossOwnerAccess        ErrorCode = "AUTH_CROSS_OWNER_ACCESS"
	ErrAuthScopeEscalation         ErrorCode = "AUTH_SCOPE_ESCALATION"
	ErrAuthMetricsTokenRequired    ErrorCode = "AUTH_METRICS_TOKEN_REQUIRED"
	ErrAuthInvalidBearerToken      ErrorCode = "AUTH_INVALID_BEARER_TOKEN"
	ErrAuthMetricsDisabled         ErrorCode = "AUTH_METRICS_DISABLED"

	// APIKEY — API-key resource management
	ErrAPIKeyNotFound      ErrorCode = "APIKEY_NOT_FOUND"
	ErrAPIKeyOwnerRequired ErrorCode = "APIKEY_OWNER_REQUIRED"
	ErrAPIKeyInvalid       ErrorCode = "APIKEY_INVALID"

	// TXN — transactions
	ErrTxnNotFound             ErrorCode = "TXN_NOT_FOUND"
	ErrTxnInsufficientFunds    ErrorCode = "TXN_INSUFFICIENT_FUNDS"
	ErrTxnInvalidAmount        ErrorCode = "TXN_INVALID_AMOUNT"
	ErrTxnPrecisionNotInteger  ErrorCode = "TXN_PRECISION_NOT_INTEGER"
	ErrTxnInvalidDistribution  ErrorCode = "TXN_INVALID_DISTRIBUTION"
	ErrTxnDuplicateReference   ErrorCode = "TXN_DUPLICATE_REFERENCE"
	ErrTxnNotInflight          ErrorCode = "TXN_NOT_INFLIGHT"
	ErrTxnAlreadyCommitted     ErrorCode = "TXN_ALREADY_COMMITTED"
	ErrTxnAlreadyVoided        ErrorCode = "TXN_ALREADY_VOIDED"
	ErrTxnAlreadyRefunded      ErrorCode = "TXN_ALREADY_REFUNDED"
	ErrTxnCommitAmountExceeded ErrorCode = "TXN_COMMIT_AMOUNT_EXCEEDED"
	ErrTxnInvalidStatusAction  ErrorCode = "TXN_INVALID_STATUS_ACTION"
	ErrTxnBulkEmpty            ErrorCode = "TXN_BULK_EMPTY"
	ErrTxnBulkLimitExceeded    ErrorCode = "TXN_BULK_LIMIT_EXCEEDED"
	ErrTxnValidation           ErrorCode = "TXN_VALIDATION_ERROR"

	// BAL — balances & monitors
	ErrBalNotFound         ErrorCode = "BAL_NOT_FOUND"
	ErrBalHistoryNotFound  ErrorCode = "BAL_HISTORY_NOT_FOUND"
	ErrBalInvalidTimestamp ErrorCode = "BAL_INVALID_TIMESTAMP"
	ErrBalValidation       ErrorCode = "BAL_VALIDATION_ERROR"
	ErrBalMonitorNotFound  ErrorCode = "BAL_MONITOR_NOT_FOUND"

	// LGR — ledgers
	ErrLgrNotFound  ErrorCode = "LGR_NOT_FOUND"
	ErrLgrDuplicate ErrorCode = "LGR_DUPLICATE"

	// ACC — accounts
	ErrAccNotFound         ErrorCode = "ACC_NOT_FOUND"
	ErrAccDuplicate        ErrorCode = "ACC_DUPLICATE"
	ErrAccGenerationFailed ErrorCode = "ACC_GENERATION_FAILED"

	// IDT — identities & tokenization
	ErrIdtNotFound              ErrorCode = "IDT_NOT_FOUND"
	ErrIdtValidation            ErrorCode = "IDT_VALIDATION_ERROR"
	ErrIdtFieldNotTokenizable   ErrorCode = "IDT_FIELD_NOT_TOKENIZABLE"
	ErrIdtFieldAlreadyTokenized ErrorCode = "IDT_FIELD_ALREADY_TOKENIZED"
	ErrIdtFieldNotTokenized     ErrorCode = "IDT_FIELD_NOT_TOKENIZED"
	ErrIdtFieldNotFound         ErrorCode = "IDT_FIELD_NOT_FOUND"
	ErrIdtTokenizationDisabled  ErrorCode = "IDT_TOKENIZATION_DISABLED"

	// RECON — reconciliation
	ErrReconNotFound               ErrorCode = "RECON_NOT_FOUND"
	ErrReconRuleNotFound           ErrorCode = "RECON_RULE_NOT_FOUND"
	ErrReconUploadFailed           ErrorCode = "RECON_UPLOAD_FAILED"
	ErrReconUploadProcessingFailed ErrorCode = "RECON_UPLOAD_PROCESSING_FAILED"
	ErrReconUploadURLInvalid       ErrorCode = "RECON_UPLOAD_URL_INVALID"
	ErrReconUploadHostNotAllowed   ErrorCode = "RECON_UPLOAD_HOST_NOT_ALLOWED"
	ErrReconRuleInvalid            ErrorCode = "RECON_RULE_INVALID"
	ErrReconMatchingRulesRequired  ErrorCode = "RECON_MATCHING_RULES_REQUIRED"
	ErrReconExternalTxnsRequired   ErrorCode = "RECON_EXTERNAL_TXNS_REQUIRED"
	ErrReconStartFailed            ErrorCode = "RECON_START_FAILED"

	// META — entity metadata
	ErrMetaEntityNotFound    ErrorCode = "META_ENTITY_NOT_FOUND"
	ErrMetaUnsupportedEntity ErrorCode = "META_UNSUPPORTED_ENTITY"
	ErrMetaInvalidEntityID   ErrorCode = "META_INVALID_ENTITY_ID"

	// HOOK — webhook management
	ErrHookNotFound        ErrorCode = "HOOK_NOT_FOUND"
	ErrHookInvalid         ErrorCode = "HOOK_INVALID"
	ErrHookOperationFailed ErrorCode = "HOOK_OPERATION_FAILED"

	// QUEUE — async processing
	ErrQueueBackpressure ErrorCode = "QUEUE_BACKPRESSURE"

	// SRCH — search & reindex
	ErrSrchQueryInvalid      ErrorCode = "SRCH_QUERY_INVALID"
	ErrSrchFailed            ErrorCode = "SRCH_FAILED"
	ErrSrchReindexInProgress ErrorCode = "SRCH_REINDEX_IN_PROGRESS"
	ErrSrchReindexNotStarted ErrorCode = "SRCH_REINDEX_NOT_STARTED"

	// ADMIN — administrative operations
	ErrAdminBackupFailed ErrorCode = "ADMIN_BACKUP_FAILED"
)

Domain-prefixed error codes. These are the canonical, client-facing codes returned in the `error_detail.code` field of every error response. Each code maps to exactly one default HTTP status (see statusByCode).

The six legacy codes in apierror.go (NOT_FOUND, CONFLICT, ...) remain valid for internal construction — they are normalized to their GEN_* equivalents at the response boundary via Normalize.

func Normalize

func Normalize(code ErrorCode) ErrorCode

Normalize converts a legacy error code to its canonical equivalent. Canonical codes pass through unchanged.

type ErrorResponse

type ErrorResponse struct {
	Error APIError `json:"error"`
}

ErrorResponse is the standard envelope for structured error payloads.

func NewErrorResponse

func NewErrorResponse(code ErrorCode, message string, details interface{}) ErrorResponse

NewErrorResponse builds an ErrorResponse with the code normalized to its canonical form. Unlike NewAPIError it does not log; callers own logging.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL