gerr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package gerr is cloudrig's canonical error type: a code, an explicit HTTP status, and the reason string clients branch on. It knows of no service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteJSON

func WriteJSON(w http.ResponseWriter, err error)

WriteJSON renders err as the GCP JSON error envelope, coercing through From so no handler can emit a body of another shape.

Types

type BadRequest

type BadRequest struct {
	FieldViolations []FieldViolation
}

BadRequest is google.rpc.BadRequest: the request itself was malformed.

type Code

type Code int32

Code is a canonical error code. Values match google.rpc.Code numerically, so a gRPC rendering is later a cast rather than a translation.

const (
	OK                 Code = 0
	Canceled           Code = 1
	Unknown            Code = 2
	InvalidArgument    Code = 3
	DeadlineExceeded   Code = 4
	NotFound           Code = 5
	AlreadyExists      Code = 6
	PermissionDenied   Code = 7
	ResourceExhausted  Code = 8
	FailedPrecondition Code = 9
	Aborted            Code = 10
	OutOfRange         Code = 11
	Unimplemented      Code = 12
	Internal           Code = 13
	Unavailable        Code = 14
	DataLoss           Code = 15
	Unauthenticated    Code = 16
)

func (Code) String

func (c Code) String() string

type Detail

type Detail interface {
	// contains filtered or unexported methods
}

Detail is a google.rpc error detail payload. These mirror the proto containers so swapping in the generated types later is mechanical.

type Envelope

type Envelope struct {
	Error EnvelopeError `json:"error"`
}

Envelope is the GCP JSON error body:

{"error":{"code":412,"message":"...","errors":[...],"status":"FAILED_PRECONDITION"}}

type EnvelopeEntry

type EnvelopeEntry struct {
	Message      string `json:"message,omitempty"`
	Domain       string `json:"domain,omitempty"`
	Reason       string `json:"reason,omitempty"`
	Location     string `json:"location,omitempty"`
	LocationType string `json:"locationType,omitempty"`
}

EnvelopeEntry is one entry in errors[]. Clients branch on Reason.

type EnvelopeError

type EnvelopeError struct {
	Code    int             `json:"code"`
	Message string          `json:"message"`
	Errors  []EnvelopeEntry `json:"errors,omitempty"`
	Status  string          `json:"status"`
}

EnvelopeError is the "error" object. Code is the HTTP status; the canonical code appears as Status.

type Error

type Error struct {
	Code    Code
	Message string

	// Reason is the lowerCamel string clients branch on: "conditionNotMet".
	Reason string

	// Location names the request element at fault; LocationType says which
	// kind it is ("header", "parameter").
	Location     string
	LocationType string

	// Domain defaults to "global" when rendered, matching GCS.
	Domain string

	Details []Detail
	// contains filtered or unexported fields
}

Error is a canonical error. Reason and Location are first-class because clients branch on errors[].reason, not on a generic detail map.

func From

func From(err error) *Error

From coerces any error into an *Error. An unclassified one becomes Internal: a failure we did not classify is our bug, not the caller's.

func New

func New(code Code, msg string) *Error

New returns an error carrying the canonical HTTP status for code. Service handlers should chain WithHTTPStatus rather than rely on that default.

func NewUnimplemented

func NewUnimplemented(op string) *Error

NewUnimplemented names the operation: a 501 that does not say what is missing is not loud enough (spec.md rule 5).

func Newf

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

Newf is New with formatting.

func Wrap

func Wrap(err error, code Code, format string, a ...any) *Error

Wrap annotates err, preserving it for errors.Is and errors.As.

func (*Error) Envelope

func (e *Error) Envelope() Envelope

Envelope renders the error into the JSON body. Reason and Location produce the primary errors[] entry; each detail violation appends another.

func (*Error) Error

func (e *Error) Error() string

func (*Error) HTTPStatus

func (e *Error) HTTPStatus() int

HTTPStatus is the explicit status if set, else the canonical mapping.

func (*Error) HTTPStatusIsExplicit

func (e *Error) HTTPStatusIsExplicit() bool

HTTPStatusIsExplicit reports whether WithHTTPStatus was called, so a service-layer check can fail the build on a handler that forgot.

func (*Error) Is

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

Is matches on code alone: errors.Is(err, gerr.New(gerr.NotFound, "")).

func (*Error) Unwrap

func (e *Error) Unwrap() error

func (*Error) With

func (e *Error) With(details ...Detail) *Error

With appends google.rpc detail payloads.

func (*Error) WithDomain

func (e *Error) WithDomain(domain string) *Error

WithDomain overrides the default "global".

func (*Error) WithHTTPStatus

func (e *Error) WithHTTPStatus(status int) *Error

WithHTTPStatus states the status this error renders as. Service handlers call it at every user-visible construction site.

func (*Error) WithLocation

func (e *Error) WithLocation(location, locationType string) *Error

WithLocation names the request element at fault; locationType is "header", "parameter" or "".

func (*Error) WithReason

func (e *Error) WithReason(reason string) *Error

WithReason sets the string clients branch on.

type ErrorInfo

type ErrorInfo struct {
	// Reason is UPPER_SNAKE here, lowerCamel'd when rendered.
	Reason   string
	Domain   string
	Metadata map[string]string
}

ErrorInfo is google.rpc.ErrorInfo: the machine-readable reason.

type FieldViolation

type FieldViolation struct {
	Field       string
	Description string
}

FieldViolation names one bad field and why.

type PreconditionFailure

type PreconditionFailure struct {
	Violations []PreconditionViolation
}

PreconditionFailure is google.rpc.PreconditionFailure: well-formed request, state did not permit it.

type PreconditionViolation

type PreconditionViolation struct {
	Type        string
	Subject     string
	Description string
}

PreconditionViolation names one unmet precondition.

Jump to

Keyboard shortcuts

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