Documentation
¶
Overview ¶
Package apierror provides structured error handling for HTTP APIs.
This package defines a standardized error interface for creating consistent, JSON-serializable API errors with metadata support. All errors implement custom JSON marshaling for consistent API responses and support fluent composition for building complex error responses.
Index ¶
- type APIError
- type DefaultAPIError
- func (e *DefaultAPIError) Data() any
- func (e *DefaultAPIError) Error() string
- func (e *DefaultAPIError) ID() string
- func (e *DefaultAPIError) Message() string
- func (e *DefaultAPIError) Origin() string
- func (e *DefaultAPIError) WithData(data any) *DefaultAPIError
- func (e *DefaultAPIError) WithID(id string) *DefaultAPIError
- func (e *DefaultAPIError) WithMessage(message string) *DefaultAPIError
- func (e *DefaultAPIError) WithOrigin(origin string) *DefaultAPIError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DefaultAPIError ¶
type DefaultAPIError struct {
ErrID string `json:"id"`
ErrData any `json:"data,omitempty"`
ErrMessage string `json:"message,omitempty"`
ErrOrigin string `json:"origin,omitempty"`
}
DefaultAPIError represents a JSON marshalable custom error type.
func APIErrorFrom ¶
func APIErrorFrom(err APIError) *DefaultAPIError
APIErrorFrom converts an APIError to a DefaultAPIError.
Parameters:
- err: The APIError to convert.
Returns:
- *DefaultAPIError: A new DefaultAPIError instance.
func NewAPIError ¶
func NewAPIError(id string) *DefaultAPIError
NewAPIError returns a new error with the given ID.
Parameters:
- id: The ID of the error.
Returns:
- *DefaultAPIError: A new DefaultAPIError instance.
func (*DefaultAPIError) Data ¶
func (e *DefaultAPIError) Data() any
Data returns the data associated with the error.
Returns:
- any: The data associated with the error.
func (*DefaultAPIError) Error ¶
func (e *DefaultAPIError) Error() string
Error returns the full error message as a string. If the error has a message, it returns the ID followed by the message. Otherwise, it returns just the ID.
Returns:
- string: The full error message as a string.
func (*DefaultAPIError) ID ¶
func (e *DefaultAPIError) ID() string
ID returns the ID of the error.
Returns:
- string: The ID of the error.
func (*DefaultAPIError) Message ¶
func (e *DefaultAPIError) Message() string
Message returns the message associated with the error.
Returns:
- string: The message associated with the error.
func (*DefaultAPIError) Origin ¶
func (e *DefaultAPIError) Origin() string
Origin returns the origin associated with the error.
Returns:
- string: The origin associated with the error.
func (*DefaultAPIError) WithData ¶
func (e *DefaultAPIError) WithData(data any) *DefaultAPIError
WithData returns a new error with the given data.
Parameters:
- data: The data to include in the error.
Returns:
- *DefaultAPIError: A new DefaultAPIError.
func (*DefaultAPIError) WithID ¶
func (e *DefaultAPIError) WithID(id string) *DefaultAPIError
WithID returns a new error with the given ID.
Parameters:
- id: The ID to include in the error.
Returns:
- *DefaultAPIError: A new DefaultAPIError.
func (*DefaultAPIError) WithMessage ¶
func (e *DefaultAPIError) WithMessage(message string) *DefaultAPIError
WithMessage returns a new error with the given message.
Parameters:
- message: The message to include in the error.
Returns:
- *DefaultAPIError: A new DefaultAPIError.
func (*DefaultAPIError) WithOrigin ¶
func (e *DefaultAPIError) WithOrigin(origin string) *DefaultAPIError
WithOrigin returns a new error with the given origin.
Parameters:
- origin: The origin to include in the error.
Returns:
- *DefaultAPIError: A new DefaultAPIError.