errors

package
v5.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package errors provides a unified error type with dual HTTP and gRPC status codes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteProblem

func WriteProblem(w http.ResponseWriter, r *http.Request, err error, requestID string)

WriteProblem writes an RFC 9457 Problem Details JSON response for the given error. It converts the error to a ServiceError via FromError, builds a ProblemDetail, and injects the requestID as an extension member if non-empty. This is the canonical write path used by httpkit and guard.

Types

type ProblemDetail

type ProblemDetail struct {
	Type       string         `json:"type"`
	Title      string         `json:"title"`
	Status     int            `json:"status"`
	Detail     string         `json:"detail"`
	Instance   string         `json:"instance,omitempty"`
	Extensions map[string]any `json:"-"` // serialized as top-level members
}

ProblemDetail represents an RFC 9457 Problem Details object. Extension members are serialized as top-level fields per the RFC spec.

func (ProblemDetail) MarshalJSON

func (pd ProblemDetail) MarshalJSON() ([]byte, error)

MarshalJSON implements custom serialization to place extension members at the top level of the JSON object, as required by RFC 9457.

type ServiceError

type ServiceError struct {
	Message  string
	GRPCCode codes.Code
	HTTPCode int
	Details  map[string]any
	// contains filtered or unexported fields
}

ServiceError represents an error with both HTTP and gRPC status codes.

func DependencyError

func DependencyError(msg string) *ServiceError

DependencyError creates an error for dependency failures (503 / UNAVAILABLE).

func Errorf

func Errorf(factory func(string) *ServiceError, format string, args ...any) *ServiceError

Errorf creates a formatted ServiceError using the given factory.

func ForbiddenError

func ForbiddenError(msg string) *ServiceError

ForbiddenError creates an error for permission denials (403 / PERMISSION_DENIED).

func FromError

func FromError(err error) *ServiceError

FromError converts any error to a ServiceError. If the error is already a ServiceError it is returned as-is; otherwise it is wrapped as internal.

func InternalError

func InternalError(msg string) *ServiceError

InternalError creates an error for unexpected failures (500 / INTERNAL).

func NotFoundError

func NotFoundError(msg string) *ServiceError

NotFoundError creates an error for missing resources (404 / NOT_FOUND).

func PayloadTooLargeError

func PayloadTooLargeError(msg string) *ServiceError

PayloadTooLargeError creates an error for oversized request bodies (413 / INVALID_ARGUMENT).

func RateLimitError

func RateLimitError(msg string) *ServiceError

RateLimitError creates an error for rate limiting (429 / RESOURCE_EXHAUSTED).

func TimeoutError

func TimeoutError(msg string) *ServiceError

TimeoutError creates an error for deadline exceeded (504 / DEADLINE_EXCEEDED).

func UnauthorizedError

func UnauthorizedError(msg string) *ServiceError

UnauthorizedError creates an error for auth failures (401 / UNAUTHENTICATED).

func ValidationError

func ValidationError(msg string) *ServiceError

ValidationError creates an error for invalid input (400 / INVALID_ARGUMENT).

func (*ServiceError) Error

func (e *ServiceError) Error() string

Error implements the error interface.

func (*ServiceError) GRPCStatus

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

GRPCStatus returns a gRPC status for this error.

func (*ServiceError) ProblemDetail

func (e *ServiceError) ProblemDetail(r *http.Request) ProblemDetail

ProblemDetail converts this ServiceError into an RFC 9457 ProblemDetail, using the request to populate the Instance field.

func (*ServiceError) Unwrap

func (e *ServiceError) Unwrap() error

Unwrap returns the underlying cause, supporting errors.Is/As chains.

func (*ServiceError) WithCause

func (e *ServiceError) WithCause(err error) *ServiceError

WithCause returns a copy of the error with the underlying error cause set for Unwrap chaining.

func (*ServiceError) WithDetail

func (e *ServiceError) WithDetail(key string, value any) *ServiceError

WithDetail returns a copy of the error with the given detail key-value pair added. The receiver is not modified, making it safe to decorate errors across goroutines.

func (*ServiceError) WithDetails

func (e *ServiceError) WithDetails(details map[string]any) *ServiceError

WithDetails returns a copy of the error with the given detail key-value pairs added. The receiver is not modified, making it safe to decorate errors across goroutines.

func (*ServiceError) WithType

func (e *ServiceError) WithType(uri string) *ServiceError

WithType returns a copy of the error with a custom RFC 9457 type URI, overriding the default.

Jump to

Keyboard shortcuts

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