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 ¶
- func WriteJSON(w http.ResponseWriter, err error)
- type BadRequest
- type Code
- type Detail
- type Envelope
- type EnvelopeEntry
- type EnvelopeError
- type Error
- func (e *Error) Envelope() Envelope
- func (e *Error) Error() string
- func (e *Error) HTTPStatus() int
- func (e *Error) HTTPStatusIsExplicit() bool
- func (e *Error) Is(target error) bool
- func (e *Error) Unwrap() error
- func (e *Error) With(details ...Detail) *Error
- func (e *Error) WithDomain(domain string) *Error
- func (e *Error) WithHTTPStatus(status int) *Error
- func (e *Error) WithLocation(location, locationType string) *Error
- func (e *Error) WithReason(reason string) *Error
- type ErrorInfo
- type FieldViolation
- type PreconditionFailure
- type PreconditionViolation
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 DataLoss Code = 15 Unauthenticated Code = 16 )
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 ¶
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 ¶
New returns an error carrying the canonical HTTP status for code. Service handlers should chain WithHTTPStatus rather than rely on that default.
func NewUnimplemented ¶
NewUnimplemented names the operation: a 501 that does not say what is missing is not loud enough (spec.md rule 5).
func (*Error) Envelope ¶
Envelope renders the error into the JSON body. Reason and Location produce the primary errors[] entry; each detail violation appends another.
func (*Error) HTTPStatus ¶
HTTPStatus is the explicit status if set, else the canonical mapping.
func (*Error) HTTPStatusIsExplicit ¶
HTTPStatusIsExplicit reports whether WithHTTPStatus was called, so a service-layer check can fail the build on a handler that forgot.
func (*Error) WithDomain ¶
WithDomain overrides the default "global".
func (*Error) WithHTTPStatus ¶
WithHTTPStatus states the status this error renders as. Service handlers call it at every user-visible construction site.
func (*Error) WithLocation ¶
WithLocation names the request element at fault; locationType is "header", "parameter" or "".
func (*Error) WithReason ¶
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 ¶
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 ¶
PreconditionViolation names one unmet precondition.