errors

package
v0.0.0-...-79528b0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 15 Imported by: 43

README

Shared Error Definitions

This directory contains centralized error definitions for all domains in the project. It is structured by feature/module to maintain clarity and separation of concerns.

Structure

Each module has its own folder with the following convention:

  • api_<module>_errors.go — for HTTP/API-related errors.
  • repo_<module>_errors.go — for repository/database-related errors.
  • service_<module>_errors.go — for service/business logic-related errors.
  • README.md — brief explanation per module.

This structure allows better error management and decouples concerns between layers (API, Service, Repository).

Modules

  • auth_errors
  • card_errors
  • merchant_errors
  • merchant_document_errors
  • refresh_token_errors
  • role_errors
  • saldo_errors
  • topup_errors
  • transaction_errors
  • transfer_errors
  • user_errors
  • user_role_errors
  • withdraw_errors

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest = &AppError{
		Type:    ErrorTypeBadRequest,
		Code:    http.StatusBadRequest,
		Message: "Bad request",
	}

	ErrValidationFailed = &AppError{
		Type:    ErrorTypeBadRequest,
		Code:    http.StatusBadRequest,
		Message: "Validation failed",
	}

	ErrUnauthorized = &AppError{
		Type:    ErrorTypeUnauthorized,
		Code:    http.StatusUnauthorized,
		Message: "Unauthorized",
	}

	ErrForbidden = &AppError{
		Type:    ErrorTypeForbidden,
		Code:    http.StatusForbidden,
		Message: "Forbidden",
	}

	ErrNotFound = &AppError{
		Type:    ErrorTypeNotFound,
		Code:    http.StatusNotFound,
		Message: "Resource not found",
	}

	ErrConflict = &AppError{
		Type:    ErrorTypeConflict,
		Code:    http.StatusConflict,
		Message: "Resource conflict",
	}

	ErrTooManyRequests = &AppError{
		Type:      ErrorTypeBadRequest,
		Code:      http.StatusTooManyRequests,
		Message:   "Too many requests",
		Retryable: true,
	}

	ErrInternal = &AppError{
		Type:    ErrorTypeInternal,
		Code:    http.StatusInternalServerError,
		Message: "Internal server error",
	}

	ErrServiceUnavailable = &AppError{
		Type:      ErrorTypeUnavailable,
		Code:      http.StatusServiceUnavailable,
		Message:   "Service unavailable",
		Retryable: true,
	}

	ErrTimeout = &AppError{
		Type:      ErrorTypeTimeout,
		Code:      http.StatusGatewayTimeout,
		Message:   "Request timeout",
		Retryable: true,
	}
)
View Source
var ErrFindByUserID = ErrNotFound.WithMessage("refresh token not found by user ID")

ErrFindByUserID is a sentinel error for when a refresh token by user ID is not found.

View Source
var ErrParseDate = ErrInternal.WithMessage("failed to parse expiration date")

ErrParseDate is returned when parsing the expiration date of a token fails.

View Source
var ErrRoleNotFound = ErrNotFound.WithMessage("role not found")

ErrRoleNotFound is a sentinel error for when a role is not found.

View Source
var ErrTokenNotFound = ErrNotFound.WithMessage("refresh token not found")

ErrTokenNotFound is a sentinel error for when a refresh token is not found.

View Source
var ErrUserNotFound = ErrNotFound.WithMessage("user not found")

ErrUserNotFound is a sentinel error for when a user is not found.

Functions

func HandleApiError

func HandleApiError(c echo.Context, err error, traceID string) error

func InvalidAccessToken

func InvalidAccessToken() error

func NewGrpcError

func NewGrpcError(message string, httpCode int) error

func ToGrpcError

func ToGrpcError(err error) error

Types

type ApiHandler

type ApiHandler interface {
	Handle(method string, handler func(echo.Context) error) echo.HandlerFunc
	HandleApiErrorWithTracing(c echo.Context, err error, span trace.Span, method string) error
}

type AppError

type AppError struct {
	Type        ErrorType         `json:"type"`
	Code        int               `json:"-"`
	Message     string            `json:"message"`
	Retryable   bool              `json:"retryable,omitempty"`
	Validations []ValidationError `json:"validations,omitempty"`
	Internal    error             `json:"-"`
}

func ErrFailed

func ErrFailed(operation string) *AppError

ErrFailed creates a generic repository error with the given operation description. This single function replaces dozens of individual error variables across all services.

func ErrNoRowsOrFailed

func ErrNoRowsOrFailed(err error, entity, operation string) *AppError

ErrNoRowsOrFailed maps a mutation error: a "no document/row" not-found error becomes a 404 not-found AppError for the given entity, and any other error becomes a generic failure for the given operation. This avoids leaking a misleading 500 INTERNAL when a mutation targets a record that does not exist (or is in the wrong state).

func ErrNotFoundResponse

func ErrNotFoundResponse(entity string) *AppError

ErrNotFoundResponse creates a not-found error response with the given entity name. The entity name should be capitalized (e.g., "User", "Transaction") for proper response formatting. This replaces individual ErrXxxNotFound variables across all services.

func NewBadRequestError

func NewBadRequestError(message string) *AppError

func NewConflictError

func NewConflictError(message string) *AppError

func NewForbiddenError

func NewForbiddenError(message string) *AppError

func NewInternalError

func NewInternalError(err error) *AppError

func NewNotFoundError

func NewNotFoundError(resource string) *AppError

func NewServiceUnavailableError

func NewServiceUnavailableError(service string) *AppError

func NewValidationError

func NewValidationError(validations []ValidationError) *AppError

func ParseGrpcError

func ParseGrpcError(err error) *AppError

func (*AppError) AsRetryable

func (e *AppError) AsRetryable() *AppError

func (*AppError) Error

func (e *AppError) Error() string

func (*AppError) Is

func (e *AppError) Is(target error) bool

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

func (*AppError) WithInternal

func (e *AppError) WithInternal(err error) *AppError

func (*AppError) WithMessage

func (e *AppError) WithMessage(message string) *AppError

func (*AppError) WithValidations

func (e *AppError) WithValidations(validations []ValidationError) *AppError

type ErrorResponse

type ErrorResponse struct {
	Status      string            `json:"status"`
	Message     string            `json:"message"`
	Type        ErrorType         `json:"type,omitempty"`
	Code        int               `json:"code"`
	TraceID     string            `json:"trace_id,omitempty"`
	Retryable   bool              `json:"retryable,omitempty"`
	Validations []ValidationError `json:"validations,omitempty"`
}

type ErrorType

type ErrorType string
const (
	ErrorTypeInternal     ErrorType = "INTERNAL"
	ErrorTypeNotFound     ErrorType = "NOT_FOUND"
	ErrorTypeBadRequest   ErrorType = "BAD_REQUEST"
	ErrorTypeConflict     ErrorType = "CONFLICT"
	ErrorTypeUnauthorized ErrorType = "UNAUTHORIZED"
	ErrorTypeForbidden    ErrorType = "FORBIDDEN"
	ErrorTypeTimeout      ErrorType = "TIMEOUT"
	ErrorTypeUnavailable  ErrorType = "UNAVAILABLE"
)

func (ErrorType) String

func (t ErrorType) String() string

type ValidationError

type ValidationError struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

Directories

Path Synopsis
card_errors
merchant_document_errors
merchant_errors
role_errors
saldo_errors
topup_errors
transaction_errors
transfer_errors
user_errors
user_role_errors
withdraw_errors

Jump to

Keyboard shortcuts

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