errors

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: Apache-2.0 Imports: 2 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 GetStatus

func GetStatus(err error) int

GetStatus returns the HTTP status code for a given error. If the error is of type *Error, it returns its Status. Otherwise, it defaults to 500 Internal Server Error.

Types

type Error

type Error struct {
	// Status is the HTTP status code associated with the error.
	Status int `json:"status"`

	// Message is a human-readable description of the error.
	Message string `json:"message"`

	// Details provides optional additional context (e.g., validation errors).
	Details interface{} `json:"details,omitempty"`

	// Cause is the underlying error that triggered this one, if any.
	// It is excluded from JSON serialization to prevent leaking internal details.
	Cause error `json:"-"`
}

Error represents a standardized application error that can be serialized to JSON. It implements the standard error interface.

func New

func New(status int, message string, args ...interface{}) *Error

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

func NewBadRequest

func NewBadRequest(message string, args ...interface{}) *Error

NewBadRequest creates a 400 Bad Request error.

func NewConflict

func NewConflict(message string, args ...interface{}) *Error

NewConflict creates a 409 Conflict error.

func NewForbidden

func NewForbidden(message string, args ...interface{}) *Error

NewForbidden creates a 403 Forbidden error.

func NewInternalServerError

func NewInternalServerError(err error, message string, args ...interface{}) *Error

NewInternalServerError creates a 500 Internal Server Error. It automatically wraps the original error.

func NewNotFound

func NewNotFound(message string, args ...interface{}) *Error

NewNotFound creates a 404 Not Found error.

func NewUnauthorized

func NewUnauthorized(message string, args ...interface{}) *Error

NewUnauthorized creates a 401 Unauthorized error.

func Wrap

func Wrap(err error, status int, message string, args ...interface{}) *Error

Wrap creates a new Error wrapping an existing error with a status code and message.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause of the error, allowing usage with errors.Is and errors.As.

func (*Error) WithDetails

func (e *Error) WithDetails(details interface{}) *Error

WithDetails adds additional details to the error (e.g., validation map) and returns the error.

Jump to

Keyboard shortcuts

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