errors

package
v1.26.2 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package errors provides a structured error handling system based on Google's error handling best practices.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeToHTTPStatus

func CodeToHTTPStatus(code Code) int

CodeToHTTPStatus maps error codes to HTTP status codes

func Is

func Is(err error, code Code) bool

Is checks if an error has a specific code

func IsAlreadyExists

func IsAlreadyExists(err error) bool

IsAlreadyExists checks if an error indicates an "already exists" condition

func IsInvalidArgument

func IsInvalidArgument(err error) bool

IsInvalidArgument checks if an error indicates an "invalid argument" condition

func IsNotFound

func IsNotFound(err error) bool

IsNotFound checks if an error indicates a "not found" condition

func Match

func Match(err error, entity string, code Code) bool

Match reports whether the error matches all the given criteria

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

func (Code) String

func (c Code) String() string

String returns the string representation of the error code

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

func AlreadyExistsError(op, entity, id string) *Error

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 Errorf

func Errorf(code Code, format string, args ...interface{}) *Error

Errorf creates a new Error with a formatted message

func InternalError

func InternalError(op, entity, id string, err error) *Error

InternalError creates a new Internal error with an underlying cause

func InvalidArgumentError

func InvalidArgumentError(op, entity, reason string) *Error

InvalidArgumentError creates a new InvalidArgument error

func NotFoundError

func NotFoundError(op, entity, id string) *Error

NotFoundError creates a new NotFound error

func Wrap

func Wrap(err error, code Code, message string) *Error

Wrap wraps an existing error with additional context

func (*Error) As

func (e *Error) As(target interface{}) bool

As implements the errors.As interface for error type conversion

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface

func (*Error) Is

func (e *Error) Is(target error) bool

Is checks if this error matches a target error This enables the use of errors.Is with this error type

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap implements the errors.Wrapper interface

func (*Error) With

func (e *Error) With(fields map[string]interface{}) *Error

With adds additional context fields to the error

type Op

type Op string

Op represents an operation in the call chain

Jump to

Keyboard shortcuts

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