errors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package errors provides unified error handling for the service layer

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHTTPStatus

func GetHTTPStatus(err error) int

GetHTTPStatus returns the HTTP status code for an error

func IsServiceError

func IsServiceError(err error) bool

IsServiceError checks if an error is a ServiceError

Types

type ErrorCode

type ErrorCode string

ErrorCode represents a unique error code

const (
	// Authentication errors (1xxx)
	ErrCodeUnauthorized     ErrorCode = "AUTH_1001"
	ErrCodeInvalidToken     ErrorCode = "AUTH_1002"
	ErrCodeTokenExpired     ErrorCode = "AUTH_1003"
	ErrCodeInvalidSignature ErrorCode = "AUTH_1004"

	// Authorization errors (2xxx)
	ErrCodeForbidden         ErrorCode = "AUTHZ_2001"
	ErrCodeInsufficientFunds ErrorCode = "AUTHZ_2002"
	ErrCodeOwnershipRequired ErrorCode = "AUTHZ_2003"

	// Validation errors (3xxx)
	ErrCodeInvalidInput     ErrorCode = "VAL_3001"
	ErrCodeMissingParameter ErrorCode = "VAL_3002"
	ErrCodeInvalidFormat    ErrorCode = "VAL_3003"
	ErrCodeOutOfRange       ErrorCode = "VAL_3004"

	// Resource errors (4xxx)
	ErrCodeNotFound      ErrorCode = "RES_4001"
	ErrCodeAlreadyExists ErrorCode = "RES_4002"
	ErrCodeConflict      ErrorCode = "RES_4003"

	// Service errors (5xxx)
	ErrCodeInternal          ErrorCode = "SVC_5001"
	ErrCodeDatabaseError     ErrorCode = "SVC_5002"
	ErrCodeBlockchainError   ErrorCode = "SVC_5003"
	ErrCodeExternalAPI       ErrorCode = "SVC_5004"
	ErrCodeTimeout           ErrorCode = "SVC_5005"
	ErrCodeRateLimitExceeded ErrorCode = "SVC_5006"

	// Cryptographic errors (6xxx)
	ErrCodeEncryptionFailed   ErrorCode = "CRYPTO_6001"
	ErrCodeDecryptionFailed   ErrorCode = "CRYPTO_6002"
	ErrCodeSigningFailed      ErrorCode = "CRYPTO_6003"
	ErrCodeVerificationFailed ErrorCode = "CRYPTO_6004"

	// TEE errors (7xxx)
	ErrCodeAttestationFailed ErrorCode = "TEE_7001"
	ErrCodeSealingFailed     ErrorCode = "TEE_7002"
	ErrCodeUnsealingFailed   ErrorCode = "TEE_7003"
)

type ServiceError

type ServiceError struct {
	Code       ErrorCode              `json:"code"`
	Message    string                 `json:"message"`
	HTTPStatus int                    `json:"-"`
	Details    map[string]interface{} `json:"details,omitempty"`
	Err        error                  `json:"-"`
}

ServiceError represents a structured error with code, message, and HTTP status

func AlreadyExists

func AlreadyExists(resource, id string) *ServiceError

func AttestationFailed

func AttestationFailed(err error) *ServiceError

func BlockchainError

func BlockchainError(operation string, err error) *ServiceError

func Conflict

func Conflict(message string) *ServiceError

func DatabaseError

func DatabaseError(operation string, err error) *ServiceError

func DecryptionFailed

func DecryptionFailed(err error) *ServiceError

func EncryptionFailed

func EncryptionFailed(err error) *ServiceError

func ExternalAPIError

func ExternalAPIError(service string, err error) *ServiceError

func Forbidden

func Forbidden(message string) *ServiceError

func GetServiceError

func GetServiceError(err error) *ServiceError

GetServiceError extracts a ServiceError from an error chain

func InsufficientFunds

func InsufficientFunds(required, available string) *ServiceError

func Internal

func Internal(message string, err error) *ServiceError

func InvalidFormat

func InvalidFormat(field, expected string) *ServiceError

func InvalidInput

func InvalidInput(field, reason string) *ServiceError

func InvalidSignature

func InvalidSignature(err error) *ServiceError

func InvalidToken

func InvalidToken(err error) *ServiceError

func MissingParameter

func MissingParameter(param string) *ServiceError

func New

func New(code ErrorCode, message string, httpStatus int) *ServiceError

New creates a new ServiceError

func NotFound

func NotFound(resource, id string) *ServiceError

func OutOfRange

func OutOfRange(field string, minValue, maxValue interface{}) *ServiceError

func OwnershipRequired

func OwnershipRequired(resource string) *ServiceError

func RateLimitExceeded

func RateLimitExceeded(limit int, window string) *ServiceError

func SealingFailed

func SealingFailed(err error) *ServiceError

func SigningFailed

func SigningFailed(err error) *ServiceError

func Timeout

func Timeout(operation string) *ServiceError

func TokenExpired

func TokenExpired() *ServiceError

func Unauthorized

func Unauthorized(message string) *ServiceError

func UnsealingFailed

func UnsealingFailed(err error) *ServiceError

func VerificationFailed

func VerificationFailed(err error) *ServiceError

func Wrap

func Wrap(code ErrorCode, message string, httpStatus int, err error) *ServiceError

Wrap wraps an existing error with a ServiceError

func (*ServiceError) Error

func (e *ServiceError) Error() string

Error implements the error interface

func (*ServiceError) Unwrap

func (e *ServiceError) Unwrap() error

Unwrap returns the underlying error

func (*ServiceError) WithDetails

func (e *ServiceError) WithDetails(key string, value interface{}) *ServiceError

WithDetails adds additional details to the error

Jump to

Keyboard shortcuts

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