apperrors
import "github.com/Ask-Atlas/AskAtlas/api/pkg/apperrors"
Package apperrors defines standard application-level errors and HTTP mappings. It provides a unified way to represent, handle, and return errors across the API.
Index
- Variables
- func RespondWithError(w http.ResponseWriter, appErr *AppError)
- type AppError
- func NewBadRequest(message string, details map[string]string) *AppError
- func NewForbidden() *AppError
- func NewInternalError() *AppError
- func NewNotFound(message string) *AppError
- func NewUnauthorized() *AppError
- func ToHTTPError(err error) *AppError
- func (e *AppError) Error() string
- func (e *AppError) Unwrap() error
Variables
Standard predefined application errors.
var (
ErrNotFound = errors.New("not found")
ErrConflict = errors.New("already exists")
ErrUnauthorized = errors.New("unauthorized")
ErrForbidden = errors.New("forbidden")
ErrInvalidInput = errors.New("invalid input")
)
func RespondWithError
func RespondWithError(w http.ResponseWriter, appErr *AppError)
RespondWithError writes the given AppError as a JSON response to the client.
type AppError
AppError represents an HTTP-friendly error containing a status code, a message, and optional validation details.
type AppError struct {
Code int `json:"code"`
Status string `json:"status"`
Message string `json:"message"`
Details map[string]string `json:"details,omitempty"`
Cause error `json:"-"`
}
func NewBadRequest
func NewBadRequest(message string, details map[string]string) *AppError
NewBadRequest creates an AppError with an HTTP 400 Bad Request status.
func NewForbidden
func NewForbidden() *AppError
NewForbidden creates an AppError with an HTTP 403 Forbidden status.
func NewInternalError
func NewInternalError() *AppError
NewInternalError creates an AppError with an HTTP 500 Internal Server Error status.
func NewNotFound
func NewNotFound(message string) *AppError
NewNotFound creates an AppError with an HTTP 404 Not Found status.
func NewUnauthorized
func NewUnauthorized() *AppError
NewUnauthorized creates an AppError with an HTTP 401 Unauthorized status.
func ToHTTPError
func ToHTTPError(err error) *AppError
ToHTTPError maps a sentinel error or existing AppError to an AppError
func (*AppError) Error
func (e *AppError) Error() string
Error returns the underlying error message.
func (*AppError) Unwrap
func (e *AppError) Unwrap() error
Unwrap returns the underlying cause of the error.
Generated by gomarkdoc