Documentation
¶
Overview ¶
Package apierrors provides HTTP error handling with structured error responses. It supports business and internal errors with customizable HTTP status codes, error codes, and logging levels.
Index ¶
Constants ¶
const (
// ErrCodeInternal is the default error code for internal service errors.
ErrCodeInternal = 900
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Error ¶
type Error struct {
ErrorCode int
ErrorMessage string
Details map[string]any `json:",omitempty"`
// contains filtered or unexported fields
}
Error represents a structured HTTP error with an error code, message, and optional details. It supports custom HTTP status codes, logging levels, and error chaining.
func New ¶
New creates a new Error with the specified HTTP status code, error code, message, and cause. The default log level is ErrorLevel.
func NewBusinessError ¶
NewBusinessError creates a new business error with HTTP 400 status code. It logs at Warn level and is used for client-side validation or business logic errors.
func NewInternalServiceError ¶
NewInternalServiceError creates a new internal service error with HTTP 500 status code. It logs at Error level and wraps the provided cause error.
func (Error) WithDetails ¶
WithDetails adds additional context details to the error.
func (Error) WithLogLevel ¶
WithLogLevel sets the logging level for this error.
func (Error) WriteError ¶
func (e Error) WriteError(w http.ResponseWriter) error
WriteError writes the error as a JSON response to the http.ResponseWriter. It sets the Content-Type header to application/json and the appropriate HTTP status code.