123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- package domain
- import (
- "errors"
- "fmt"
- "github.com/nicksnyder/go-i18n/v2/i18n"
- )
- type ErrorKind = string
- var NoError = i18n.Message{ID: "error.none", Other: "No error"}
- var Unimplemented = i18n.Message{ID: "error.unimplemented", Other: "Not yet implemented"}
- var ShortNotCreated = i18n.Message{ID: "error.short_not_created", Other: "Short URL not created for a new target"}
- var TargetInvalid = i18n.Message{ID: "error.target_invalid", Other: "Target invalid"}
- var ShortNotFound = i18n.Message{ID: "error.short_not_found", Other: "Short URL not defined"}
- var TargetBlocked = i18n.Message{ID: "error.target_blocked", Other: "Target blocked"}
- var TargetCensored = i18n.Message{ID: "error.target_censored", Other: "Target unavailable for legal reasons"}
- var StorageRead = i18n.Message{ID: "error.storage_read", Other: "Storage read error"}
- var StorageWrite = i18n.Message{ID: "error.storage_write", Other: "Storage write error"}
- var StorageUnspecified = i18n.Message{ID: "error.storage_unspecified", Other: "Storage unspecified error"}
- var ErrorMessages = map[ErrorKind]i18n.Message{
- NoError.ID: NoError,
- Unimplemented.ID: Unimplemented,
- ShortNotCreated.ID: ShortNotCreated,
- ShortNotFound.ID: ShortNotFound,
- TargetBlocked.ID: TargetBlocked,
- TargetCensored.ID: TargetCensored,
- TargetInvalid.ID: TargetInvalid,
- StorageRead.ID: StorageRead,
- StorageWrite.ID: StorageWrite,
- StorageUnspecified.ID: StorageUnspecified,
- }
- // Error instances are created localized by MakeError(kind, detail, localizer),
- // so they do not need to support localization themselves.
- type Error struct {
- error
- Kind ErrorKind
- }
- // MakeError creates a localized error instance, ready to print.
- func MakeError(l *i18n.Localizer, kind ErrorKind, detail string) Error {
- var message string
- // Support errors with a localizer.
- if l == nil {
- s := ErrorMessages[kind].Other
- var e error
- if detail != "" {
- e = errors.New(fmt.Sprintf("%s: %s", s, detail))
- } else {
- e = errors.New(s)
- }
- err := Error{
- Kind: kind,
- error: e,
- }
- return err
- }
- if len(detail) == 0 {
- message = l.MustLocalize(&i18n.LocalizeConfig{
- MessageID: ErrorMessages[kind].ID,
- })
- } else {
- em, ok := ErrorMessages[kind]
- if !ok {
- em = i18n.Message{
- ID: "error.dynamic",
- Description: "This is an unplanned error generated on the fly from details. It should be replaced",
- }
- }
- localizedKind := l.MustLocalize(&i18n.LocalizeConfig{
- DefaultMessage: &em,
- MessageID: em.ID,
- })
- message = l.MustLocalize(&i18n.LocalizeConfig{
- DefaultMessage: &i18n.Message{
- ID: "error.error_format",
- Description: "The format for errors",
- One: "{.Kind}: {.Detail}",
- LeftDelim: "{",
- RightDelim: "}",
- },
- TemplateData: map[string]string{
- "Kind": localizedKind,
- "Detail": detail,
- },
- PluralCount: 1,
- })
- }
- return Error{
- Kind: kind,
- error: errors.New(message),
- }
- }
|