Documentation
¶
Overview ¶
Package apperrors provides typed domain errors with HTTP status mapping.
Domain errors are defined in the domain layer without any HTTP knowledge. The server middleware maps AppError types to HTTP status codes automatically.
Usage in domain layer:
var ErrUserNotFound = apperrors.NotFound("user not found")
var ErrEmailTaken = apperrors.Conflict("email already taken")
Usage in server middleware:
status := apperrors.HTTPStatus(err) // 404, 409, etc.
Package apperrors provides typed domain errors with HTTP status mapping.
Overview ¶
Define errors in the domain layer without any HTTP knowledge. The server middleware maps AppError types to HTTP status codes automatically.
Defining domain errors ¶
package users
import "github.com/miilkaa/portsmith/pkg/apperrors"
var (
ErrUserNotFound = apperrors.NotFound("user not found")
ErrEmailTaken = apperrors.Conflict("email already taken")
)
Error comparison ¶
Use errors.Is for sentinel error comparison — it works across error chains:
if errors.Is(err, users.ErrUserNotFound) { ... }
HTTP mapping ¶
The server error middleware calls apperrors.HTTPStatus automatically. Explicit use is rarely needed:
status := apperrors.HTTPStatus(err) // 404, 409, 400, 403, 401, 500
Error codes ¶
CodeNotFound → 404 CodeConflict → 409 CodeBadRequest → 400 CodeForbidden → 403 CodeUnauthorized → 401 CodeInternal → 500
Index ¶
- func As(err error, target **AppError) bool
- func HTTPStatus(err error) int
- func IsAppError(err error) bool
- func IsCode(err error, code Code) bool
- type AppError
- func BadRequest(msg string) *AppError
- func Conflict(msg string) *AppError
- func Forbidden(msg string) *AppError
- func Internal(msg string, args ...any) *AppError
- func NotFound(msg string) *AppError
- func Unauthorized(msg string) *AppError
- func WithDetails(err *AppError, details map[string]any) *AppError
- type Code
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HTTPStatus ¶
HTTPStatus maps an AppError code to the corresponding HTTP status code. Returns 500 for any unrecognised code or non-AppError errors.
Types ¶
type AppError ¶
type AppError struct {
// contains filtered or unexported fields
}
AppError is a typed domain error with an associated code and optional details. Use errors.Is for sentinel error comparison.
func BadRequest ¶
BadRequest creates a BAD_REQUEST domain error (maps to HTTP 400).
func Unauthorized ¶
Unauthorized creates an UNAUTHORIZED domain error (maps to HTTP 401).
func WithDetails ¶
WithDetails returns a copy of the AppError enriched with structured details. The resulting error still matches the original via errors.Is.