errors

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 5 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

This section is empty.

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]string `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]string
	// 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 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 sets the underlying error cause for Unwrap chaining.

func (*ServiceError) WithDetail

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

WithDetail adds a single detail key-value pair (fluent).

func (*ServiceError) WithDetails

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

WithDetails adds multiple detail key-value pairs at once (fluent).

func (*ServiceError) WithType

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

WithType sets 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