Documentation
¶
Overview ¶
Package errors provides a structured error handling system based on Google's error handling best practices.
Index ¶
- func CodeToHTTPStatus(code Code) int
- func Is(err error, code Code) bool
- func IsAlreadyExists(err error) bool
- func IsInvalidArgument(err error) bool
- func IsNotFound(err error) bool
- func Match(err error, entity string, code Code) bool
- type Code
- type Error
- func AlreadyExistsError(op, entity, id string) *Error
- func E(args ...interface{}) *Error
- func Errorf(code Code, format string, args ...interface{}) *Error
- func InternalError(op, entity, id string, err error) *Error
- func InvalidArgumentError(op, entity, reason string) *Error
- func NotFoundError(op, entity, id string) *Error
- func Wrap(err error, code Code, message string) *Error
- type Op
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CodeToHTTPStatus ¶
CodeToHTTPStatus maps error codes to HTTP status codes
func IsAlreadyExists ¶
IsAlreadyExists checks if an error indicates an "already exists" condition
func IsInvalidArgument ¶
IsInvalidArgument checks if an error indicates an "invalid argument" condition
func IsNotFound ¶
IsNotFound checks if an error indicates a "not found" condition
Types ¶
type Code ¶
type Code string
Code defines the type of error
const ( // OK indicates success (no error) OK Code = "OK" // Canceled indicates the operation was canceled Canceled Code = "CANCELED" // Unknown indicates an unknown or unclassified error Unknown Code = "UNKNOWN" // InvalidArgument indicates the caller specified an invalid argument InvalidArgument Code = "INVALID_ARGUMENT" // NotFound indicates the requested entity was not found NotFound Code = "NOT_FOUND" // AlreadyExists indicates an attempt to create an entity that already exists AlreadyExists Code = "ALREADY_EXISTS" // PermissionDenied indicates the caller does not have permission PermissionDenied Code = "PERMISSION_DENIED" // Unauthenticated indicates the request lacks valid authentication credentials Unauthenticated Code = "UNAUTHENTICATED" // ResourceExhausted indicates resource quota or limits were exceeded ResourceExhausted Code = "RESOURCE_EXHAUSTED" // FailedPrecondition indicates the system is not in a state required for the operation FailedPrecondition Code = "FAILED_PRECONDITION" // Internal indicates an internal error has occurred Internal Code = "INTERNAL" )
Application-wide error codes
type Error ¶
type Error struct {
// Code is one of the error codes defined in code.go
Code Code
// Entity is the type of entity being operated on (e.g., "Issue")
Entity string
// ID is the ID of the entity (if applicable)
ID string
// Op is the operation being performed (e.g., "issueHandler.GetIssue")
Op string
// Message is a human-readable description of the error
Message string
// Err is the underlying error that triggered this one
Err error
// Fields contains additional structured metadata about the error
Fields map[string]interface{}
}
Error is the fundamental error type for the application
func AlreadyExistsError ¶
AlreadyExistsError creates a new AlreadyExists error
func E ¶
func E(args ...interface{}) *Error
E creates a new Error from the provided arguments. Each argument can be an Op, a string, a Code, an error, or a map[string]interface{}.
func InternalError ¶
InternalError creates a new Internal error with an underlying cause
func InvalidArgumentError ¶
InvalidArgumentError creates a new InvalidArgument error
func NotFoundError ¶
NotFoundError creates a new NotFound error
func (*Error) Is ¶
Is checks if this error matches a target error This enables the use of errors.Is with this error type