result

package
v0.38.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
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).

View Source
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

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.

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

func IsRecordNotFound(err error) bool

IsRecordNotFound checks if the error is a record not found error.

Types

type ErrOption

type ErrOption func(*Error)

ErrOption configures an Error.

func WithCode

func WithCode(code int) ErrOption

WithCode sets the business error code.

func WithStatus

func WithStatus(status int) ErrOption

WithStatus sets the HTTP status code.

type Error

type Error struct {
	Code    int
	Message string
	Status  int
}

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 AsErr

func AsErr(err error) (Error, bool)

AsErr extracts an Error from err if present.

func Err

func Err(messageOrOptions ...any) Error

Err creates a new Error with optional message and options. Usage: Err(), Err("message"), Err("message", WithCode(...)), Err(WithCode(...)).

func ErrNotImplemented

func ErrNotImplemented(message string) Error

ErrNotImplemented creates a not implemented error with custom message (HTTP 501).

func Errf

func Errf(format string, args ...any) Error

Errf creates a new Error with a formatted message. Usage: Errf("user %s not found", username), Errf("error %d", code, WithCode(...)).

func (Error) Error

func (e Error) Error() string

Error implements the error interface.

func (Error) Is added in v0.25.1

func (e Error) Is(target error) bool

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

func WithMessage(message string) OkOption

WithMessage sets a custom message for the result.

func WithMessagef

func WithMessagef(format string, args ...any) OkOption

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

func Ok(dataOrOptions ...any) Result

Ok creates a success Result with optional data and options. Usage: Ok(), Ok(data), Ok(WithMessage(...)), Ok(data, WithMessage(...)).

func (Result) IsOk

func (r Result) IsOk() bool

IsOk returns true if the result code indicates success.

func (Result) Response

func (r Result) Response(ctx fiber.Ctx, status ...int) error

Response sends the result as JSON with optional HTTP status (defaults to 200).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL