errors

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Status

func Status(code Code) int

func TraceID

func TraceID(ctx context.Context) string

TraceID returns trace_id from OpenTelemetry span context. If there's no valid span context, it returns empty string. NOTE: request_id is NOT a trace_id and must not be used as fallback.

func Write

func Write(ctx context.Context, w http.ResponseWriter, status int, err *Error)

Write writes a JSON error response. It guarantees: - content-type is application/json; charset=utf-8 - details is always an object (never null) - trace_id is injected from OTel span context if missing

func WriteError

func WriteError(ctx context.Context, w http.ResponseWriter, err *Error)

Types

type Code

type Code string
const (
	// Generic
	CodeInternal    Code = "INTERNAL"
	CodeUnavailable Code = "UNAVAILABLE"

	// Auth
	CodeUnauthenticated Code = "UNAUTHENTICATED"
	CodeForbidden       Code = "FORBIDDEN"

	// Validation / client
	CodeInvalidArgument   Code = "INVALID_ARGUMENT"
	CodeNotFound          Code = "NOT_FOUND"
	CodeConflict          Code = "CONFLICT"
	CodeTooManyRequests   Code = "TOO_MANY_REQUESTS"
	CodeResourceExhausted Code = "RESOURCE_EXHAUSTED"

	// Optional (recommended for core/orders/payments)
	CodeDeadlineExceeded   Code = "DEADLINE_EXCEEDED"
	CodeAlreadyExists      Code = "ALREADY_EXISTS"
	CodeFailedPrecondition Code = "FAILED_PRECONDITION"
)

func (Code) String

func (c Code) String() string

type Error

type Error struct {
	Code    Code           `json:"code"`
	Message string         `json:"message"`
	Details map[string]any `json:"details"`
	TraceID string         `json:"trace_id"`
}

func AlreadyExists

func AlreadyExists(message string) *Error

func As

func As(err error) (*Error, bool)

func Conflict

func Conflict(message string) *Error

func DeadlineExceeded

func DeadlineExceeded(message string) *Error

func FailedPrecondition

func FailedPrecondition(message string) *Error

func Forbidden

func Forbidden(message string) *Error

func Internal

func Internal(message string) *Error

func InvalidArgument

func InvalidArgument(message string) *Error

func New

func New(code Code, message string, details map[string]any) *Error

func NotFound

func NotFound(message string) *Error

func ResourceExhausted

func ResourceExhausted(message string) *Error

func Unauthenticated

func Unauthenticated(message string) *Error

func Unavailable

func Unavailable(message string) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) MarshalJSON

func (e *Error) MarshalJSON() ([]byte, error)

func (*Error) WithTrace

func (e *Error) WithTrace(ctx context.Context) *Error

WithTrace returns a shallow copy of e with TraceID derived from OTel span context. Details map is cloned to keep the error immutable from caller mutations.

Jump to

Keyboard shortcuts

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