Files
kotosoft-id/kotosoft-id-backend/internal/models/error.go
T
2026-03-09 12:50:29 +03:00

49 lines
1.5 KiB
Go

package models
type APIError struct {
Code int `json:"-"`
Message string `json:"message"`
Slug string `json:"error"`
Cause error `json:"-"`
}
var (
ErrBadRequest = &APIError{Code: 400, Slug: "bad_request", Message: "Invalid request"}
ErrUnauthorized = &APIError{Code: 401, Slug: "unauthorized", Message: "Incorrect credentials"}
ErrForbidden = &APIError{Code: 403, Slug: "forbidden", Message: "Forbidden"}
ErrInsufficientScope = &APIError{Code: 403, Slug: "insufficient_scope", Message: "The access token does not contain the required scope"}
ErrNotFound = &APIError{Code: 404, Slug: "not_found", Message: "Resource not found"}
ErrAlreadyExists = &APIError{Code: 409, Slug: "already_exists", Message: "Resource already exists"}
ErrTooManyRequests = &APIError{Code: 429, Slug: "too_many_requests", Message: "Too many requests"}
ErrInternal = &APIError{Code: 500, Slug: "internal", Message: "Internal server error"}
)
func (e *APIError) Error() string {
return e.Slug
}
func (e *APIError) WithMessage(msg string) *APIError {
if e == nil {
return (&APIError{Code: 500, Slug: "internal", Message: "Internal server error"}).WithMessage(msg)
}
cp := *e
cp.Message = msg
return &cp
}
func (e *APIError) WithCause(cause error) *APIError {
if e == nil {
return (&APIError{Code: 500, Slug: "internal", Message: "Internal server error"}).WithCause(cause)
}
cp := *e
cp.Cause = cause
return &cp
}
func (e *APIError) Unwrap() error {
if e == nil {
return nil
}
return e.Cause
}