result

package
v0.19.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	OkMessage                                 = "ok"
	ErrMessage                                = "error"
	ErrMessageRecordNotFound                  = "record_not_found"
	ErrMessageRecordAlreadyExists             = "record_already_exists"
	ErrMessageForeignKeyViolation             = "foreign_key_violation"
	ErrMessageUnknown                         = "unknown_error"
	ErrMessageNotFound                        = "not_found"
	ErrMessageTooManyRequests                 = "too_many_requests"
	ErrMessageUnauthenticated                 = "unauthenticated"
	ErrMessageTokenExpired                    = "token_expired"
	ErrMessageTokenInvalid                    = "token_invalid"
	ErrMessageTokenNotValidYet                = "token_not_valid_yet"
	ErrMessageTokenInvalidIssuer              = "token_invalid_issuer"
	ErrMessageTokenInvalidAudience            = "token_invalid_audience"
	ErrMessageTokenMissingSubject             = "token_missing_subject"
	ErrMessageTokenMissingTokenType           = "token_missing_token_type"
	ErrMessageAppIDRequired                   = "app_id_required"
	ErrMessageTimestampRequired               = "timestamp_required"
	ErrMessageSignatureRequired               = "signature_required"
	ErrMessageTimestampInvalid                = "timestamp_invalid"
	ErrMessageSignatureExpired                = "signature_expired"
	ErrMessageExternalAppNotFound             = "external_app_not_found"
	ErrMessageExternalAppDisabled             = "external_app_disabled"
	ErrMessageIPNotAllowed                    = "ip_not_allowed"
	ErrMessageSignatureInvalid                = "signature_invalid"
	ErrMessageAccessDenied                    = "access_denied"
	ErrMessageUnsupportedMediaType            = "unsupported_media_type"
	ErrMessageRequestTimeout                  = "request_timeout"
	ErrMessageMonitorNotReady                 = "monitor_not_ready"
	ErrMessageInvalidFileKey                  = "invalid_file_key"
	ErrMessageFileNotFound                    = "file_not_found"
	ErrMessageFailedToGetFile                 = "failed_to_get_file"
	ErrMessageApiRequestParamsInvalidJSON     = "api_request_params_invalid_json"
	ErrMessageApiRequestMetaInvalidJSON       = "api_request_meta_invalid_json"
	ErrMessageDangerousSQL                    = "dangerous_sql"
	ErrMessageExternalAppLoaderNotImplemented = "external_app_loader_not_implemented"
	ErrMessageCredentialsFormatInvalid        = "credentials_format_invalid"
	ErrMessageCredentialsFieldsRequired       = "credentials_fields_required"
	ErrMessageSignatureDecodeFailed           = "signature_decode_failed"
	ErrMessageNonceRequired                   = "nonce_required"
	ErrMessageNonceInvalid                    = "nonce_invalid"
	ErrMessageNonceAlreadyUsed                = "nonce_already_used"
	ErrMessageAuthHeaderMissing               = "auth_header_missing"
	ErrMessageAuthHeaderInvalid               = "auth_header_invalid"
	ErrMessageUnsupportedAuthenticationType   = "unsupported_authentication_type"
	ErrMessageUserLoaderNotImplemented        = "user_loader_not_implemented"
	ErrMessageUserInfoLoaderNotImplemented    = "user_info_loader_not_implemented"
)

i18n message keys for API responses.

View Source
const (
	OkCode = 0

	// Authentication errors (1000-1099).
	ErrCodeUnauthenticated               = 1000
	ErrCodeUnsupportedAuthenticationType = 1001
	ErrCodeTokenExpired                  = 1002
	ErrCodeTokenInvalid                  = 1003
	ErrCodeTokenNotValidYet              = 1004
	ErrCodeTokenInvalidIssuer            = 1005
	ErrCodeTokenInvalidAudience          = 1007
	ErrCodeTokenMissingSubject           = 1008
	ErrCodeTokenMissingTokenType         = 1009
	ErrCodePrincipalInvalid              = 1010
	ErrCodeCredentialsInvalid            = 1011
	ErrCodeAppIDRequired                 = 1012
	ErrCodeTimestampRequired             = 1013
	ErrCodeSignatureRequired             = 1014
	ErrCodeTimestampInvalid              = 1015
	ErrCodeSignatureExpired              = 1016
	ErrCodeExternalAppNotFound           = 1017
	ErrCodeExternalAppDisabled           = 1018
	ErrCodeIPNotAllowed                  = 1019
	ErrCodeSignatureInvalid              = 1020
	ErrCodeNonceRequired                 = 1021
	ErrCodeNonceInvalid                  = 1022
	ErrCodeNonceAlreadyUsed              = 1023
	ErrCodeAuthHeaderMissing             = 1024
	ErrCodeAuthHeaderInvalid             = 1025

	// 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
	ErrCodeMonitorNotReady     = 2100
	ErrCodeInvalidFileKey      = 2200
	ErrCodeFileNotFound        = 2201
	ErrCodeSchemaTableNotFound = 2300
)

Response codes for API results. Code 0 indicates success; codes 1000+ indicate authentication/authorization errors; codes 2000+ indicate business logic errors.

Variables

Predefined authentication errors (HTTP 401).

View Source
var (
	ErrAppIDRequired = Err(
		i18n.T(ErrMessageAppIDRequired),
		WithCode(ErrCodeAppIDRequired),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrTimestampRequired = Err(
		i18n.T(ErrMessageTimestampRequired),
		WithCode(ErrCodeTimestampRequired),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrSignatureRequired = Err(
		i18n.T(ErrMessageSignatureRequired),
		WithCode(ErrCodeSignatureRequired),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrTimestampInvalid = Err(
		i18n.T(ErrMessageTimestampInvalid),
		WithCode(ErrCodeTimestampInvalid),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrSignatureExpired = Err(
		i18n.T(ErrMessageSignatureExpired),
		WithCode(ErrCodeSignatureExpired),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrSignatureInvalid = Err(
		i18n.T(ErrMessageSignatureInvalid),
		WithCode(ErrCodeSignatureInvalid),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrExternalAppNotFound = Err(
		i18n.T(ErrMessageExternalAppNotFound),
		WithCode(ErrCodeExternalAppNotFound),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrExternalAppDisabled = Err(
		i18n.T(ErrMessageExternalAppDisabled),
		WithCode(ErrCodeExternalAppDisabled),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrIPNotAllowed = Err(
		i18n.T(ErrMessageIPNotAllowed),
		WithCode(ErrCodeIPNotAllowed),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrNonceRequired = Err(
		i18n.T(ErrMessageNonceRequired),
		WithCode(ErrCodeNonceRequired),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrNonceInvalid = Err(
		i18n.T(ErrMessageNonceInvalid),
		WithCode(ErrCodeNonceInvalid),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrNonceAlreadyUsed = Err(
		i18n.T(ErrMessageNonceAlreadyUsed),
		WithCode(ErrCodeNonceAlreadyUsed),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrAuthHeaderMissing = Err(
		i18n.T(ErrMessageAuthHeaderMissing),
		WithCode(ErrCodeAuthHeaderMissing),
		WithStatus(fiber.StatusUnauthorized),
	)
	ErrAuthHeaderInvalid = Err(
		i18n.T(ErrMessageAuthHeaderInvalid),
		WithCode(ErrCodeAuthHeaderInvalid),
		WithStatus(fiber.StatusUnauthorized),
	)
)

Predefined external app authentication errors (HTTP 401).

Predefined authorization and request errors.

Predefined business errors (HTTP 200 with error code).

Functions

func IsRecordNotFound added in v0.8.0

func IsRecordNotFound(err error) bool

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

Types

type ErrOption added in v0.18.0

type ErrOption func(*Error)

ErrOption configures an Error.

func WithCode added in v0.8.0

func WithCode(code int) ErrOption

WithCode sets the business error code.

func WithStatus added in v0.8.0

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 ErrCredentialsInvalid added in v0.18.0

func ErrCredentialsInvalid(message string) Error

ErrCredentialsInvalid creates a credentials invalid error with custom message (HTTP 401).

func ErrNotImplemented added in v0.18.0

func ErrNotImplemented(message string) Error

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

func ErrPrincipalInvalid added in v0.18.0

func ErrPrincipalInvalid(message string) Error

ErrPrincipalInvalid creates a principal invalid error with custom message (HTTP 401).

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.

type OkOption added in v0.18.0

type OkOption func(*Result)

OkOption configures a Result.

func WithMessage added in v0.8.0

func WithMessage(message string) OkOption

WithMessage sets a custom message for the result.

func WithMessagef added in v0.8.4

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