forked from mtusi2026/mtusi2026lab2
123 lines
2.7 KiB
Go
123 lines
2.7 KiB
Go
package apperr
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
)
|
|
|
|
// Code is the machine-readable error code used by the API contract.
|
|
type Code string
|
|
|
|
const (
|
|
CodeInvalidRequest Code = "invalid_request"
|
|
CodeUnauthorized Code = "unauthorized"
|
|
CodeNotFound Code = "not_found"
|
|
CodeConflict Code = "conflict"
|
|
CodePayloadTooLarge Code = "payload_too_large"
|
|
CodeUnsupportedMediaType Code = "unsupported_media_type"
|
|
CodeRateLimited Code = "rate_limited"
|
|
CodeUnavailable Code = "unavailable"
|
|
CodeInternalError Code = "internal_error"
|
|
|
|
CodeValidation Code = CodeInvalidRequest
|
|
CodeBadRequest Code = CodeInvalidRequest
|
|
CodeInternal Code = CodeInternalError
|
|
)
|
|
|
|
func (c Code) HTTPStatus() int {
|
|
switch c {
|
|
case CodeInvalidRequest:
|
|
return http.StatusBadRequest
|
|
case CodeUnauthorized:
|
|
return http.StatusUnauthorized
|
|
case CodeNotFound:
|
|
return http.StatusNotFound
|
|
case CodeConflict:
|
|
return http.StatusConflict
|
|
case CodePayloadTooLarge:
|
|
return http.StatusRequestEntityTooLarge
|
|
case CodeUnsupportedMediaType:
|
|
return http.StatusUnsupportedMediaType
|
|
case CodeRateLimited:
|
|
return http.StatusTooManyRequests
|
|
case CodeUnavailable:
|
|
return http.StatusServiceUnavailable
|
|
default:
|
|
return http.StatusInternalServerError
|
|
}
|
|
}
|
|
|
|
// AppError adds a public message and API code to an optional underlying error.
|
|
type AppError struct {
|
|
code Code
|
|
message string
|
|
cause error
|
|
}
|
|
|
|
func New(code Code, message string) *AppError {
|
|
return &AppError{code: code, message: message}
|
|
}
|
|
|
|
// Wrap returns nil for a nil cause.
|
|
func Wrap(err error, code Code, message string) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
return &AppError{code: code, message: message, cause: err}
|
|
}
|
|
|
|
func (e *AppError) Error() string {
|
|
if e == nil {
|
|
return "<nil>"
|
|
}
|
|
if e.cause != nil {
|
|
return e.message + ": " + e.cause.Error()
|
|
}
|
|
return e.message
|
|
}
|
|
|
|
func (e *AppError) Unwrap() error {
|
|
if e == nil {
|
|
return nil
|
|
}
|
|
return e.cause
|
|
}
|
|
|
|
func (e *AppError) Code() Code {
|
|
if e == nil {
|
|
return CodeInternalError
|
|
}
|
|
return e.code
|
|
}
|
|
|
|
func (e *AppError) Message() string {
|
|
if e == nil {
|
|
return ""
|
|
}
|
|
return e.message
|
|
}
|
|
|
|
// As finds an AppError even when it is wrapped with fmt.Errorf("...: %w", err).
|
|
func As(err error) (*AppError, bool) {
|
|
var appErr *AppError
|
|
if !errors.As(err, &appErr) || appErr == nil {
|
|
return nil, false
|
|
}
|
|
return appErr, true
|
|
}
|
|
|
|
// Root is retained for existing HTTP handler call sites.
|
|
func Root(err error) *AppError {
|
|
appErr, _ := As(err)
|
|
return appErr
|
|
}
|
|
|
|
// PublicMessage never exposes an unknown error or the cause of a server error.
|
|
func PublicMessage(err error) string {
|
|
appErr, ok := As(err)
|
|
if !ok || appErr.Code().HTTPStatus() >= http.StatusInternalServerError {
|
|
return "internal server error"
|
|
}
|
|
return appErr.Message()
|
|
}
|