Documentation
¶
Index ¶
Constants ¶
const ( OkMessage = "ok" ErrMessage = "error" // HTTP-level generic errors. ErrMessageUnknown = "unknown_error" ErrMessageNotFound = "not_found" ErrMessageTooManyRequests = "too_many_requests" ErrMessageAccessDenied = "access_denied" ErrMessageUnsupportedMediaType = "unsupported_media_type" ErrMessageRequestTimeout = "request_timeout" // ORM / persistence errors that any module may surface. ErrMessageRecordNotFound = "record_not_found" ErrMessageRecordAlreadyExists = "record_already_exists" ErrMessageForeignKeyViolation = "foreign_key_violation" ErrMessageDangerousSQL = "dangerous_sql" )
i18n message keys for cross-cutting API responses. Module-specific keys live next to their module (e.g. security.ErrMessageTokenInvalid, monitor.ErrMessageMonitorNotReady).
const ( OkCode = 0 // Authorization errors (1100-1199). ErrCodeAccessDenied = 1100 // Resource errors (1200-1299). ErrCodeNotFound = 1200 // Media type errors (1300-1399). ErrCodeUnsupportedMediaType = 1300 // Request errors (1400-1499). ErrCodeBadRequest = 1400 ErrCodeTooManyRequests = 1401 ErrCodeRequestTimeout = 1402 // Not implemented (1500-1599). ErrCodeNotImplemented = 1500 // SQL errors (1600-1699). ErrCodeDangerousSQL = 1600 // Unknown errors (1900-1999). ErrCodeUnknown = 1900 // Business errors (2000+). ErrCodeDefault = 2000 ErrCodeRecordNotFound = 2001 ErrCodeRecordAlreadyExists = 2002 ErrCodeForeignKeyViolation = 2003 )
Response codes for cross-cutting API results. Code 0 indicates success; codes 1100-1599 are HTTP-level concerns; codes 1900+ are unknown / business errors.
Variables ¶
var ( ErrAccessDenied = Err( i18n.T(ErrMessageAccessDenied), WithCode(ErrCodeAccessDenied), WithStatus(fiber.StatusForbidden), ) ErrTooManyRequests = Err( i18n.T(ErrMessageTooManyRequests), WithCode(ErrCodeTooManyRequests), WithStatus(fiber.StatusTooManyRequests), ) ErrRequestTimeout = Err( i18n.T(ErrMessageRequestTimeout), WithCode(ErrCodeRequestTimeout), WithStatus(fiber.StatusRequestTimeout), ) ErrUnknown = Err( i18n.T(ErrMessageUnknown), WithCode(ErrCodeUnknown), WithStatus(fiber.StatusInternalServerError), ) )
Predefined authorization and request errors.
Every sentinel below resolves its message through i18n.T at package-init time, capturing the language selected by VEF_I18N_LANGUAGE. These are cross-cutting sentinels that any module may surface; callers match them with errors.Is, which compares Code alone, so the frozen Message never affects identity. Switching i18n language at runtime (e.g. via i18n.SetLanguage in tests) will not update these frozen messages — new translations only take effect on process restart.
var ( ErrRecordNotFound = Err( i18n.T(ErrMessageRecordNotFound), WithCode(ErrCodeRecordNotFound), ) ErrRecordAlreadyExists = Err( i18n.T(ErrMessageRecordAlreadyExists), WithCode(ErrCodeRecordAlreadyExists), ) ErrForeignKeyViolation = Err( i18n.T(ErrMessageForeignKeyViolation), WithCode(ErrCodeForeignKeyViolation), ) ErrDangerousSQL = Err( i18n.T(ErrMessageDangerousSQL), WithCode(ErrCodeDangerousSQL), ) )
Predefined ORM/persistence errors (HTTP 200 with error code) that any module may surface. The init-time i18n freeze documented above applies to these sentinels too.
Functions ¶
func IsRecordNotFound ¶
IsRecordNotFound checks if the error is a record not found error.
Types ¶
type Error ¶
Error represents a business-level error for API responses. It separates transport-level concerns (HTTP Status) from business logic (Code, Message). HTTP Status typically remains 200 to indicate successful communication, while Code indicates the actual business result.
func Err ¶
Err creates a new Error with optional message and options. Usage: Err(), Err("message"), Err("message", WithCode(...)), Err(WithCode(...)).
func ErrNotImplemented ¶
ErrNotImplemented creates a not implemented error with custom message (HTTP 501).
func Errf ¶
Errf creates a new Error with a formatted message. Usage: Errf("user %s not found", username), Errf("error %d", code, WithCode(...)).
func (Error) Is ¶ added in v0.25.1
Is reports whether target represents the same business error as e.
Two result.Error values are considered equal when they share the same Code — Message may differ (factories such as ErrCredentialsInvalid build dynamic messages) and Status is transport-level metadata that must not affect identity comparisons. Returning true for matching Codes lets errors.Is(err, sentinel) work for both static sentinels and dynamically constructed errors from the same logical kind.
type OkOption ¶
type OkOption func(*Result)
OkOption configures a Result.
func WithMessage ¶
WithMessage sets a custom message for the result.
func WithMessagef ¶
WithMessagef sets a formatted message for the result.
type Result ¶
type Result struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data"`
}
Result represents an API response with code, message, and optional data.
func Ok ¶
Ok creates a success Result with optional data and options. Usage: Ok(), Ok(data), Ok(WithMessage(...)), Ok(data, WithMessage(...)).