Documentation
¶
Index ¶
- func FromValidatorErr(err error) error
- func GetBundle() *i18n.Bundle
- func GetCode(err error) (string, bool)
- func GetMessageKey(err error) (string, bool)
- func GetUserMessage(err error, lang string) (string, error)
- func LoadBundleFromFiles(defaultLang language.Tag, files ...string) (*i18n.Bundle, error)
- func New(code, messageKey string, httpCode int, opts ...Option) error
- func RegisterMessages(bundle *i18n.Bundle, lang string, messages map[string]string) error
- func RegisterValidatorTagMessages(bundle *i18n.Bundle, lang string, tagMessages map[string]string) error
- func SetBundle(b *i18n.Bundle) error
- func ValidatorMessageKeyForFieldError(fe validator.FieldError) string
- func Wrap(err error, code, messageKey string, httpCode int, opts ...Option) error
- type Gerr
- type Metadata
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromValidatorErr ¶
FromValidatorErr converts an error produced by github.com/go-playground/validator/v10 into a gerr error (returns the built-in error interface). Behavior:
- If err is nil, returns nil.
- If err is a validator.ValidationErrors (or contains it in its chain), this uses only the first FieldError to construct an error with:
- Code = "validation.failed"
- MessageKey = "validation.<tag>" (e.g. "validation.required")
- HTTP status = 400
- Retryable = false
- Args and Meta populated with field/tag/param info
- If err is not a validation error, returns nil.
func GetMessageKey ¶
GetMessageKey extracts the MessageKey from any error chain, if present.
func GetUserMessage ¶
GetUserMessage extracts the UserMessage from any error chain, if present.
func LoadBundleFromFiles ¶
LoadBundleFromFiles creates an i18n.Bundle with the given defaultLang (e.g. "en") and loads all provided message files. Supported file formats: JSON and YAML. The filenames' extensions (.json, .yaml, .yml) are used to determine the format.
Example:
bundle, err := LoadBundleFromFiles("en", "example/locales/en.json", "example/locales/id.json")
func New ¶
New constructs a new error (built-in error interface) representing a gerr. Parameters:
- code: machine-friendly code, e.g. "GENERIC-001" or "USER-001"
- messageKey: i18n message key, e.g. "error.user_not_found"
- httpCode: suggested HTTP status code
- opts: optional Option helpers (WithArgs, WithMeta, WithRetryable)
The function returns the standard built-in error interface. Callers that need access to gerr-specific features (Localize, accessors) should use AsError / errors.As to obtain the exported Error interface.
func RegisterMessages ¶
RegisterMessages registers arbitrary messageID->template pairs into the provided bundle for the given language. This helper is generic and can be used to register any message IDs.
Example:
RegisterMessages(bundle, "en", map[string]string{
"error.unavailable": "Service '{{.Resource}}' is unavailable",
})
func RegisterValidatorTagMessages ¶
func RegisterValidatorTagMessages(bundle *i18n.Bundle, lang string, tagMessages map[string]string) error
RegisterValidatorTagMessages registers a map of validator tag -> template text into the provided i18n.Bundle for the given language. Each entry will be registered under the message ID "validation.<tag>".
Example:
RegisterValidatorTagMessages(bundle, "en", map[string]string{
"required": "{{.Field}} is required",
"email": "{{.Field}} must be a valid email address",
})
func ValidatorMessageKeyForFieldError ¶
func ValidatorMessageKeyForFieldError(fe validator.FieldError) string
ValidatorMessageKeyForFieldError returns the message key that should be used for the provided FieldError. The convention used is "validation.<tag>". If the tag is empty, falls back to "validation.failed".
Types ¶
type Gerr ¶
type Option ¶
type Option func(*gerr)
Option configures a newly created gerr instance.
func WithArgs ¶
WithArgs sets the template args to be used for localization. It overwrites any existing args map.
func WithMetadata ¶
WithMetadata adds a metadata key/value pair. It appends the metadata to the existing metadata map.
func WithRetryable ¶
WithRetryable explicitly sets retryable flag (overrides HTTP-based default).