Documentation
¶
Overview ¶
Package errors provides semantic and traceable error management for the go-kit ecosystem.
It defines failure identity through stable error codes, human-readable messages, structured context, and cause chaining. Errors produced by this package carry enough information to be logged, serialized, and programmatically handled without coupling to any transport layer.
Example usage:
// Create a typed error directly.
err := errors.New(errors.ErrInvalidInput, "username is required")
fmt.Println(err.Error())
// Output: INVALID_ARGUMENT: username is required
// Wrap an existing error with context.
wrapped := errors.Wrap(errors.ErrInternal, "failed to save user", originalErr).
WithContext("user_id", 42)
// Coerce any error into the library's canonical type.
typed := errors.From(someErr)
fmt.Println(typed.GetCode())
Index ¶
- type Err
- func (e *Err) Detailed() string
- func (e *Err) Error() string
- func (e *Err) GetCode() string
- func (e *Err) GetContext() map[string]any
- func (e *Err) MarshalJSON() ([]byte, error)
- func (e *Err) Unwrap() error
- func (e *Err) WithContext(key string, value any) *Err
- func (e *Err) WithError(err error) *Err
- type Error
- type ErrorCode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Err ¶
type Err struct {
// contains filtered or unexported fields
}
Err represents a semantic application error with a stable code, human-readable message, optional underlying cause, and structured context for tracing.
func From ¶
From converts any error into a *Err. If err is nil, nil is returned. If the error is already a *Err (or wraps one), it is returned as-is. Otherwise, it is wrapped with ErrInternal and the original error preserved as the cause.
func Internal ¶
Internal creates a new Err with ErrInternal code. It accepts a format string and arguments for the message.
func InvalidInput ¶
InvalidInput creates a new Err with ErrInvalidInput code. It accepts a format string and arguments for the message.
func NotFound ¶
NotFound creates a new Err with ErrResourceNotFound code. It accepts a format string and arguments for the message.
func (*Err) Detailed ¶
Detailed returns a more verbose string representation of the error, including its code, message, cause, and full context.
func (*Err) Error ¶
Error returns a formatted string representation of the error. It includes the code, the message, and the underlying error if present.
func (*Err) GetContext ¶
GetContext returns the contextual data associated with the error. If no context exists, it returns an empty map.
func (*Err) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Err) Unwrap ¶
Unwrap returns the underlying cause of the error, satisfying the errors.Unwrap convention and enabling errors.Is / errors.As traversal.
func (*Err) WithContext ¶
WithContext returns a new *Err with the given key-value pair added to the context. The original error is not modified.
type Error ¶
type Error interface {
// error satisfies the built-in error interface.
error
// GetCode returns the machine-readable error code as a string.
GetCode() string
// GetContext returns the structured key-value metadata attached to the error.
// Always returns a non-nil map.
GetContext() map[string]any
// Detailed returns a verbose, structured representation suitable for logging.
Detailed() string
}
Error is the canonical interface for all errors produced by this package. Consumers such as loggers or middleware should accept this interface rather than the concrete *Err type to avoid coupling.
type ErrorCode ¶
type ErrorCode string
ErrorCode represents a unique identifier for a specific type of error. These codes are stable, transport-agnostic identifiers. Mapping to protocol-specific representations (e.g. HTTP status codes, gRPC codes) is the responsibility of the transport layer.
const ( // ErrInvalidInput indicates that the provided input is malformed or does not meet // validation requirements. ErrInvalidInput ErrorCode = "INVALID_ARGUMENT" ErrUnauthorized ErrorCode = "UNAUTHENTICATED" // ErrPermissionDenied indicates that the caller's identity is known but lacks the // required permissions. ErrPermissionDenied ErrorCode = "PERMISSION_DENIED" // ErrResourceNotFound indicates that a requested resource does not exist or is not // accessible. ErrResourceNotFound ErrorCode = "NOT_FOUND" // ErrConflict indicates a state conflict, such as a duplicate or a violated // uniqueness constraint. ErrConflict ErrorCode = "ALREADY_EXISTS" // ErrPreconditionFailed indicates that the operation was rejected because the // system is not in a state required to execute it (e.g. enabling a resource that // must first be configured). ErrPreconditionFailed ErrorCode = "FAILED_PRECONDITION" // ErrRateLimited indicates that the caller has exceeded an allowed quota or rate // and must back off before retrying. ErrRateLimited ErrorCode = "RESOURCE_EXHAUSTED" // ErrInternal indicates an unexpected failure within the system. ErrInternal ErrorCode = "INTERNAL_ERROR" // ErrNotImplemented indicates that the requested operation has not been implemented. ErrNotImplemented ErrorCode = "NOT_IMPLEMENTED" ErrUnavailable ErrorCode = "SERVICE_UNAVAILABLE" // ErrDeadlineExceeded indicates that the operation did not complete within the // allowed time. ErrDeadlineExceeded ErrorCode = "TIMEOUT" )
func (ErrorCode) Description ¶
Description returns a human-readable description for the error code. If the code is unknown, it returns the code itself as a string.