// SPDX-License-Identifier: AGPL-3.0-or-later // Package apperr defines the errors business logic returns to the HTTP layer: // each carries an HTTP-friendly kind, a stable code and a human message. package apperr import "errors" type Kind int const ( KindInvalid Kind = iota + 1 KindForbidden KindNotFound KindConflict ) type Error struct { Kind Kind Code string Message string // Fields holds per-field messages for KindInvalid. Fields map[string]string } func (e *Error) Error() string { return e.Code + ": " + e.Message } func Forbidden(msg string) *Error { return &Error{Kind: KindForbidden, Code: "forbidden", Message: msg} } func NotFound(msg string) *Error { return &Error{Kind: KindNotFound, Code: "not_found", Message: msg} } func Conflict(msg string) *Error { return &Error{Kind: KindConflict, Code: "conflict", Message: msg} } func Invalid(msg string) *Error { return &Error{Kind: KindInvalid, Code: "invalid", Message: msg} } // InvalidFields is a validation error with a message per field. func InvalidFields(fields map[string]string) *Error { return &Error{Kind: KindInvalid, Code: "validation", Message: "validation failed", Fields: fields} } // As returns err as an *Error when it is one. func As(err error) (*Error, bool) { var e *Error ok := errors.As(err, &e) return e, ok }