Documentation
¶
Index ¶
- Variables
- func ErrHandle(logger *slog.Logger, handler func(http.ResponseWriter, *http.Request) error) http.Handler
- func Permanent(err error) error
- type Code
- type ShareableError
- func (e *ShareableError) AsGoa() *goa.ServiceError
- func (e *ShareableError) Error() string
- func (e *ShareableError) HTTPStatus() int
- func (e *ShareableError) IsTemporary() bool
- func (e *ShareableError) Log(ctx context.Context, logger *slog.Logger, args ...slog.Attr) *ShareableError
- func (e *ShareableError) LogValue() slog.Value
- func (e *ShareableError) MarshalJSON() ([]byte, error)
- func (e *ShareableError) MarshalText() (text []byte, err error)
- func (e *ShareableError) String() string
- func (e *ShareableError) Unwrap() error
Constants ¶
This section is empty.
Variables ¶
var ErrPermanent = &permanentError{}
var StatusCodes = map[Code]int{ CodeUnauthorized: http.StatusUnauthorized, CodeForbidden: http.StatusForbidden, CodeBadRequest: http.StatusBadRequest, CodeNotFound: http.StatusNotFound, CodeConflict: http.StatusConflict, CodeUnsupportedMedia: http.StatusUnsupportedMediaType, CodeInvalid: http.StatusUnprocessableEntity, CodeUnexpected: http.StatusInternalServerError, CodeInvariantViolation: http.StatusUnprocessableEntity, CodeGatewayError: http.StatusBadGateway, CodeNotImplemented: http.StatusNotImplemented, }
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.
Types ¶
type Code ¶
type Code string
const ( 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 (Code) UserMessage ¶
type ShareableError ¶
type ShareableError struct {
// 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.