forked from mtusi2026/mtusi2026lab2
Реализация на 4
This commit is contained in:
@@ -2,90 +2,96 @@ package apperr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"fmt"
|
||||
|
||||
grpcCodes "google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
// 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
|
||||
err error
|
||||
msg string
|
||||
code Code
|
||||
meta metadata
|
||||
}
|
||||
|
||||
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
|
||||
func New(code Code, msg string) error {
|
||||
return &AppError{
|
||||
msg: msg,
|
||||
code: code,
|
||||
meta: metadata{},
|
||||
}
|
||||
return &AppError{code: code, message: message, cause: err}
|
||||
}
|
||||
|
||||
func Wrap(err error, code Code, msg string) error {
|
||||
m := AppError{
|
||||
err: err,
|
||||
msg: msg,
|
||||
code: code,
|
||||
meta: metadata{},
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func WrapAppError(err error, msg string) error {
|
||||
m := AppError{
|
||||
err: err,
|
||||
msg: msg,
|
||||
code: CodeInternal,
|
||||
meta: metadata{},
|
||||
}
|
||||
|
||||
if mistake := As(err); mistake != nil {
|
||||
m.err = mistake
|
||||
m.code = mistake.code
|
||||
m.meta = mistake.meta
|
||||
}
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func As(err error) *AppError {
|
||||
if mistakeErr, ok := errors.AsType[*AppError](err); ok {
|
||||
return mistakeErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CodeIs(err error, code Code) bool {
|
||||
if appErr := As(err); appErr != nil && appErr.Code() == code {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
if e == nil {
|
||||
return "<nil>"
|
||||
return ""
|
||||
}
|
||||
if e.cause != nil {
|
||||
return e.message + ": " + e.cause.Error()
|
||||
|
||||
if e.err == nil {
|
||||
if e.msg != "" {
|
||||
return e.msg
|
||||
}
|
||||
return e.code.String()
|
||||
}
|
||||
return e.message
|
||||
|
||||
if e.msg == "" {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s: %s", e.msg, e.err.Error())
|
||||
}
|
||||
|
||||
func (e *AppError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.cause
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *AppError) Code() Code {
|
||||
if e == nil {
|
||||
return CodeInternalError
|
||||
return CodeInternal
|
||||
}
|
||||
return e.code
|
||||
}
|
||||
@@ -94,29 +100,24 @@ func (e *AppError) Message() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return e.message
|
||||
return e.msg
|
||||
}
|
||||
|
||||
// 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
|
||||
func (e *AppError) Cause() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return appErr, true
|
||||
return e.err
|
||||
}
|
||||
|
||||
// Root is retained for existing HTTP handler call sites.
|
||||
func Root(err error) *AppError {
|
||||
appErr, _ := As(err)
|
||||
return appErr
|
||||
func (e *AppError) Metadata() Metadata {
|
||||
return e.meta
|
||||
}
|
||||
|
||||
// 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()
|
||||
func (e *AppError) HTTPStatus() int {
|
||||
return e.Code().HTTPStatus()
|
||||
}
|
||||
|
||||
func (e *AppError) GRPCCode() grpcCodes.Code {
|
||||
return e.Code().GRPCCode()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user