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 InitBundle(defaultLang language.Tag, files ...string) error
- func LoadBundleFromFiles(defaultLang language.Tag, paths ...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 InitBundle ¶ added in v1.0.2
InitBundle creates an i18n.Bundle with the given default language, loads the provided locale files, and sets the result as the package-level bundle.
This is the recommended single call to set up i18n at application startup. Clients are responsible for providing all locale files, including translations for any message keys used by external packages (e.g. errcodes).
Example:
gerr.InitBundle(language.English, "locales/en.json", "locales/id.json")
func LoadBundleFromFiles ¶
LoadBundleFromFiles creates an i18n.Bundle with the given defaultLang (e.g. "en") and loads all provided paths. Each path may be a file or a directory; directories are walked recursively and all .json, .yaml, and .yml files are loaded.
Example:
bundle, err := LoadBundleFromFiles(language.English, "locales/") bundle, err := LoadBundleFromFiles(language.English, "locales/en.json", "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).