errors

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: MIT Imports: 2 Imported by: 0

README

Package: $pkg

Status: 🚧 Under Development

This package will be implemented in the coming days.

Planned Features

  • TBD

Usage

// Coming soon

Documentation

Full documentation will be available after implementation.

Documentation

Overview

Package errors provides standardized error handling for LaResto microservices. It defines common error types, HTTP status code mapping, and error wrapping utilities.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadRequest indicates invalid request data
	ErrBadRequest = &AppError{
		Code:    "BAD_REQUEST",
		Message: "Invalid request data",
		Status:  http.StatusBadRequest,
	}

	// ErrUnauthorized indicates authentication is required
	ErrUnauthorized = &AppError{
		Code:    "UNAUTHORIZED",
		Message: "Authentication required",
		Status:  http.StatusUnauthorized,
	}

	// ErrForbidden indicates the user lacks permission
	ErrForbidden = &AppError{
		Code:    "FORBIDDEN",
		Message: "Permission denied",
		Status:  http.StatusForbidden,
	}

	// ErrNotFound indicates the requested resource was not found
	ErrNotFound = &AppError{
		Code:    "NOT_FOUND",
		Message: "Resource not found",
		Status:  http.StatusNotFound,
	}

	// ErrConflict indicates a conflict with existing data
	ErrConflict = &AppError{
		Code:    "CONFLICT",
		Message: "Resource already exists",
		Status:  http.StatusConflict,
	}

	// ErrValidation indicates request validation failed
	ErrValidation = &AppError{
		Code:    "VALIDATION_ERROR",
		Message: "Request validation failed",
		Status:  http.StatusBadRequest,
	}

	// ErrInternal indicates an unexpected internal error
	ErrInternal = &AppError{
		Code:    "INTERNAL_ERROR",
		Message: "An unexpected error occurred",
		Status:  http.StatusInternalServerError,
	}

	// ErrDatabase indicates a database operation failed
	ErrDatabase = &AppError{
		Code:    "DATABASE_ERROR",
		Message: "Database operation failed",
		Status:  http.StatusInternalServerError,
	}

	// ErrCache indicates a cache operation failed
	ErrCache = &AppError{
		Code:    "CACHE_ERROR",
		Message: "Cache operation failed",
		Status:  http.StatusInternalServerError,
	}

	// ErrExternal indicates an external service call failed
	ErrExternal = &AppError{
		Code:    "EXTERNAL_SERVICE_ERROR",
		Message: "External service unavailable",
		Status:  http.StatusBadGateway,
	}
)

Predefined error types for common scenarios

Functions

func GetStatus

func GetStatus(err error) int

GetStatus extracts the HTTP status code from an error. Returns 500 if the error is not an AppError.

func IsAppError

func IsAppError(err error) bool

IsAppError checks if an error is an AppError.

Types

type AppError

type AppError struct {
	// Code is a machine-readable error code (e.g., "VALIDATION_ERROR", "NOT_FOUND")
	Code string `json:"code"`

	// Message is a human-readable error message
	Message string `json:"message"`

	// Status is the HTTP status code associated with this error
	Status int `json:"-"`

	// Err is the underlying error (not exposed in JSON responses)
	Err error `json:"-"`

	// Details provides additional context (optional)
	Details map[string]interface{} `json:"details,omitempty"`
}

AppError represents a standardized application error with HTTP status mapping. It contains an error code for client identification, a human-readable message, HTTP status code, and the underlying error for logging.

func New

func New(code, message string, status int) *AppError

New creates a new AppError with the given code, message, and status.

func Wrap

func Wrap(err error, message string) *AppError

Wrap wraps an existing error with additional context. If err is already an AppError, it returns it unchanged. Otherwise, it creates a new internal error wrapping the original.

func WrapCache

func WrapCache(err error) *AppError

WrapCache wraps a cache error with context.

func WrapDB

func WrapDB(err error) *AppError

WrapDB wraps a database error with context.

func WrapExternal

func WrapExternal(err error) *AppError

WrapExternal wraps an external service error with context.

func (*AppError) Error

func (e *AppError) Error() string

Error implements the error interface.

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap returns the underlying error for error chain support.

func (*AppError) WithDetails

func (e *AppError) WithDetails(details map[string]interface{}) *AppError

WithDetails adds additional context to the error.

Jump to

Keyboard shortcuts

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