Help us shape the future of Go! Take the 2026 Go Developer Survey

error

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CodeInvalidArgument indicates invalid argument provided
	CodeInvalidArgument = "invalid_argument"
	// CodeNotFound indicates resource not found
	CodeNotFound = "not_found"
	// CodeAlreadyExists indicates resource already exists
	CodeAlreadyExists = "already_exists"
	// CodePermissionDenied indicates permission denied
	CodePermissionDenied = "permission_denied"
	// CodeUnauthenticated indicates authentication required
	CodeUnauthenticated = "unauthenticated"
	// CodeResourceExhausted indicates resource exhausted (e.g., rate limit)
	CodeResourceExhausted = "resource_exhausted"
	// CodeFailedPrecondition indicates precondition failed
	CodeFailedPrecondition = "failed_precondition"
	// CodeAborted indicates operation aborted
	CodeAborted = "aborted"
	// CodeInternal indicates internal server error
	CodeInternal = "internal"
	// CodeUnavailable indicates service unavailable
	CodeUnavailable = "unavailable"
	// CodeValidationFailed indicates validation failed
	CodeValidationFailed = "validation_failed"
)

Common error codes

Variables

This section is empty.

Functions

func Is

func Is(err error, code string) bool

Is checks if the target error is an Error with the given code.

func IsValidationError

func IsValidationError(err error) bool

IsValidationError checks if an error is a validation error.

func TranslateError

func TranslateError(err error) error

TranslateError translates an error to a gRPC status error. It handles both structured Error types and standard Go errors.

Types

type Error

type Error struct {
	// Code is a machine-readable error code.
	Code string
	// Message is a human-readable error message.
	Message string
	// Cause is the underlying error that triggered this error.
	Cause error
	// Metadata contains additional key-value pairs for context.
	Metadata map[string]any
}

Error represents a structured application error.

func New

func New(code, message string) *Error

New creates a new Error with the given code and message.

func Newf

func Newf(code, format string, args ...any) *Error

Newf creates a new Error with the given code and formatted message.

func Wrap

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

Wrap wraps an existing error with a code and message.

func Wrapf

func Wrapf(err error, code, format string, args ...any) *Error

Wrapf wraps an existing error with a code and formatted message.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

func (*Error) GRPCCode

func (e *Error) GRPCCode() codes.Code

GRPCCode returns the gRPC status code that best represents this error.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying error for error wrapping compatibility.

func (*Error) WithMetadata

func (e *Error) WithMetadata(key string, value any) *Error

WithMetadata adds metadata to the error.

type ValidationError

type ValidationError struct {
	Field   string
	Message string
}

ValidationError represents a validation error with field-specific details.

func NewValidationError

func NewValidationError(field, message string) *ValidationError

NewValidationError creates a new validation error for a single field.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Error implements the error interface.

type ValidationErrors

type ValidationErrors []ValidationError

ValidationErrors is a collection of validation errors.

func NewValidationErrors

func NewValidationErrors(errors ...ValidationError) ValidationErrors

NewValidationErrors creates a collection of validation errors.

func (ValidationErrors) Error

func (e ValidationErrors) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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