oops

package
v0.0.0-...-4ae6852 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2025 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPermanent = &permanentError{}

Functions

func ErrHandle

func ErrHandle(logger *slog.Logger, handler func(http.ResponseWriter, *http.Request) error) http.Handler

ErrHandle returns a middleware that wraps an http handler. It calls the underlying handler and captures any returned error. The error is appropriately serialized back to the client. It recognizes ShareableError and goa.ServiceError types. Any other error types are treated as internal server errors and a generic message is returned to the client. All errors are logged with the provided logger.

func Permanent

func Permanent(err error) error

Types

type Code

type Code string
const (
	CodeUnauthorized       Code = "unauthorized"
	CodeForbidden          Code = "forbidden"
	CodeBadRequest         Code = "bad_request"
	CodeNotFound           Code = "not_found"
	CodeConflict           Code = "conflict"
	CodeUnsupportedMedia   Code = "unsupported_media"
	CodeInvalid            Code = "invalid"
	CodeUnexpected         Code = "unexpected"
	CodeInvariantViolation Code = "invariant_violation"
	CodeGatewayError       Code = "gateway_error"
	CodeNotImplemented     Code = "not_implemented"
)

func (Code) IsTemporary

func (c Code) IsTemporary() bool

func (Code) UserMessage

func (c Code) UserMessage() string

type ShareableError

type ShareableError struct {
	Code Code
	// contains filtered or unexported fields
}

ShareableError is an error that can be shared with clients. It contains a public-facing message and an underlying cause that is not shared. This error type is designed to be used within service methods that are at the HTTP server boundary.

func C

func C(code Code) *ShareableError

C creates a new ShareableError with the given code and a public message derived from the code. It is a convenience function to quickly create errors with canned messages.

func E

func E(code Code, cause error, public string, args ...any) *ShareableError

E creates a new ShareableError with the given code, cause, public message, and optional formatting arguments to interpolate into the public message. The cause can be nil if there is no underlying error to capture.

func (*ShareableError) AsGoa

func (e *ShareableError) AsGoa() *goa.ServiceError

AsGoa converts the ShareableError to a goa.ServiceError, preserving the error code, id, and cause. It also sets the timeout, temporary, and fault flags based on the error code and cause.

func (*ShareableError) Error

func (e *ShareableError) Error() string

Error implements the error interface.

func (*ShareableError) HTTPStatus

func (e *ShareableError) HTTPStatus() int

HTTPStatus returns the appropriate HTTP status code for the error based on its code. If the code is not recognized, it defaults to 500 Internal Server Error.

func (*ShareableError) IsTemporary

func (e *ShareableError) IsTemporary() bool

func (*ShareableError) Log

func (e *ShareableError) Log(ctx context.Context, logger *slog.Logger, args ...slog.Attr) *ShareableError

Log logs the error using the provided logger and context. It also sets the OpenTelemetry span status to error. Additional arguments can be provided to include more context in the log entry.

func (*ShareableError) LogValue

func (e *ShareableError) LogValue() slog.Value

LogValue implements the slog.LogValuer interface.

func (*ShareableError) MarshalJSON

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

MarshalJSON implements the json.Marshaler interface.

func (*ShareableError) MarshalText

func (e *ShareableError) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (*ShareableError) String

func (e *ShareableError) String() string

String returns a detailed string representation of the error, including the private message and the cause, if any.

func (*ShareableError) Unwrap

func (e *ShareableError) Unwrap() error

Unwrap returns the underlying cause of the error, if any.

Jump to

Keyboard shortcuts

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