errors

package
v0.1.0-eff5d23 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewErrorf

func NewErrorf(code ErrorCode, format string, a ...interface{}) error

NewErrorf creates a new Error without wrapping an existing one.

func WrapError

func WrapError(original error, code ErrorCode, format string, a ...interface{}) error

WrapError wraps an existing error into the unified Error type.

Types

type Error

type Error struct {
	Original         error
	Message          string
	ErrCode          ErrorCode
	FieldViolations  []*FieldViolation
	ErrorDetailsFunc GRPCDetailsFunc
}

Error is the unified application error type. It supports gRPC status details, HTTP status mapping, and validation field violations.

func NewValidationError

func NewValidationError(violations []*FieldViolation, msg ...string) *Error

NewValidationError builds an InvalidArgument error with validation details.

func (*Error) Code

func (e *Error) Code() ErrorCode

func (*Error) Error

func (e *Error) Error() string

func (*Error) GRPCCode

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

GRPCCode maps the internal error code to a gRPC status code.

func (*Error) GRPCStatus

func (e *Error) GRPCStatus() *status.Status

GRPCStatus converts the error into a gRPC status, with field or metadata details.

func (*Error) HTTPCode

func (e *Error) HTTPCode() int

HTTPCode maps the internal error code to an HTTP status code.

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() (int, map[string]any)

HTTPStatus returns a standardized HTTP status code and JSON-safe error body. This is meant to be transport-agnostic, usable across multiple HTTP/REST frameworks.

func (*Error) Unwrap

func (e *Error) Unwrap() error

type ErrorCode

type ErrorCode int
const (
	Unknown ErrorCode = iota + 1
	NotFound
	InvalidArgument
	Internal
	Unauthenticated
	Unauthorized
	Conflict
	QuotaExceeded
	BadRequest
	NotImplemented
	ServiceUnavailable
	DeadlineExceeded
	PreconditionFailed
	TooEarly
	FailedDependency
	Gone
	UnprocessableEntity
	PayloadTooLarge
	UnsupportedMediaType
)

type FieldViolation

type FieldViolation struct {
	Field       string
	Description string
}

FieldViolation represents a validation field violation.

func BuildViolations

func BuildViolations(verrs validator.ValidationErrors) []*FieldViolation

BuildViolations converts validator.ValidationErrors into []*FieldViolation.

type GRPCDetailsFunc

type GRPCDetailsFunc func(st *status.Status, details ...protoiface.MessageV1) (*status.Status, error)

GRPCDetailsFunc allows customizing how error details are attached to gRPC response. This is particularly useful with gRPC rich error reporting.

Jump to

Keyboard shortcuts

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