apperror

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

apperror/apperror.go

Index

Constants

View Source
const (
	EnvDevelopment = "development"
	EnvStaging     = "staging"
	EnvProduction  = "production"
)

APP_ENV values

Variables

This section is empty.

Functions

func As

func As(err error, target any) bool

func HTTPMiddleware

func HTTPMiddleware(next http.Handler) http.Handler

HTTPMiddleware catches panics and converts to JSON 500

func Is

func Is(err, target error) bool

Is/As shortcuts updated to check all registered errors

func OnError

func OnError(hook func(*AppError))

func Register

func Register(name string, e *AppError) error

Register adds a named error; fails if name exists

func Unregister

func Unregister(name string) error

Unregister removes a named error

func Update

func Update(name string, e *AppError) error

Update replaces an existing named error; fails if not found

func WriteJSONError

func WriteJSONError(w http.ResponseWriter, r *http.Request, err error)

WriteJSONError writes an error as JSON, includes X-Request-ID, hides details in production

Types

type AppError

type AppError struct {
	Code       string         `json:"code"`               // 9-digit code: XXX|AA|DD|YY
	Message    string         `json:"message"`            // human-readable message
	StatusCode int            `json:"-"`                  // HTTP status, not serialized
	Err        error          `json:"-"`                  // wrapped error, not serialized
	Metadata   map[string]any `json:"metadata,omitempty"` // optional extra info
	StackTrace []string       `json:"stackTrace,omitempty"`
}

AppError defines a structured application error

func Get

func Get(name string) (*AppError, bool)

Get retrieves a named error

func GetByCode

func GetByCode(code string) (*AppError, bool)

GetByCode retrieves an error by its 9-digit code

func Instance

func Instance(e *AppError) *AppError

New helper: Instance attaches the runtime stack trace to a prototype error.

func List

func List() []*AppError

List returns all registered errors

func New

func New(httpCode, appCode, domainCode, errCode int, msg string) *AppError

New creates a fresh AppError

func Wrap

func Wrap(err error, httpCode, appCode, domainCode, errCode int, msg string) *AppError

Modify Wrap to always capture a fresh stack trace.

func (*AppError) Error

func (e *AppError) Error() string

Error implements error interface

func (*AppError) GetStackTraceArray

func (e *AppError) GetStackTraceArray() []string

GetStackTraceArray returns the error stack trace as an array of strings

func (*AppError) GetStackTraceString

func (e *AppError) GetStackTraceString() string

GetStackTraceString returns the error stack trace as a single string

func (*AppError) Unwrap

func (e *AppError) Unwrap() error

Unwrap enables errors.Is / errors.As

func (*AppError) WithMetadata

func (e *AppError) WithMetadata(key string, val any) *AppError

WithMetadata returns a shallow copy with added metadata key/value

type ErrorRegistry

type ErrorRegistry struct {
	// contains filtered or unexported fields
}

func (*ErrorRegistry) Delete

func (er *ErrorRegistry) Delete(name string)

func (*ErrorRegistry) Get

func (er *ErrorRegistry) Get(name string) (*AppError, bool)

func (*ErrorRegistry) GetByCode

func (er *ErrorRegistry) GetByCode(code string) (*AppError, bool)

func (*ErrorRegistry) List

func (er *ErrorRegistry) List() []*AppError

func (*ErrorRegistry) Set

func (er *ErrorRegistry) Set(name string, e *AppError)

Jump to

Keyboard shortcuts

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