errors.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package domain
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/nicksnyder/go-i18n/v2/i18n"
  6. )
  7. type ErrorKind = string
  8. var NoError = i18n.Message{ID: "error.none", Other: "No error"}
  9. var Unimplemented = i18n.Message{ID: "error.unimplemented", Other: "Not yet implemented"}
  10. var ShortNotCreated = i18n.Message{ID: "short_not_created", Other: "Short URL not created for a new target"}
  11. var TargetInvalid = i18n.Message{ID: "error.target_invalid", Other: "Target invalid"}
  12. var ShortNotFound = i18n.Message{ID: "short_not_found", Other: "Short URL not defined"}
  13. var TargetBlocked = i18n.Message{ID: "target_blocked", Other: "Target blocked"}
  14. var TargetCensored = i18n.Message{ID: "target_censored", Other: "Target unavailable for legal reasons"}
  15. var StorageRead = i18n.Message{ID: "storage_read", Other: "Storage read error"}
  16. var StorageWrite = i18n.Message{ID: "storage_write", Other: "Storage write error"}
  17. var StorageUnspecified = i18n.Message{ID: "storage_unspecified", Other: "Storage unspecified error"}
  18. var ErrorMessages = map[ErrorKind]i18n.Message{
  19. NoError.ID: NoError,
  20. Unimplemented.ID: Unimplemented,
  21. ShortNotCreated.ID: ShortNotCreated,
  22. ShortNotFound.ID: ShortNotFound,
  23. TargetBlocked.ID: TargetBlocked,
  24. TargetCensored.ID: TargetCensored,
  25. StorageRead.ID: StorageRead,
  26. StorageWrite.ID: StorageWrite,
  27. StorageUnspecified.ID: StorageUnspecified,
  28. }
  29. // Error instances are created localized by MakeError(kind, detail, localizer),
  30. // so they do not need to support localization themselves.
  31. type Error struct {
  32. error
  33. Kind ErrorKind
  34. }
  35. // MakeError creates a localized error instance, ready to print.
  36. func MakeError(l *i18n.Localizer, kind ErrorKind, detail string) Error {
  37. var message string
  38. // Support errors with a localizer.
  39. if l == nil {
  40. s := ErrorMessages[kind].Other
  41. var e error
  42. if detail != "" {
  43. e = errors.New(fmt.Sprintf("%s: %s", s, detail))
  44. } else {
  45. e = errors.New(s)
  46. }
  47. err := Error{
  48. Kind: kind,
  49. error: e,
  50. }
  51. return err
  52. }
  53. if len(detail) == 0 {
  54. message = l.MustLocalize(&i18n.LocalizeConfig{
  55. MessageID: ErrorMessages[kind].ID,
  56. })
  57. } else {
  58. em, ok := ErrorMessages[kind]
  59. if !ok {
  60. em = i18n.Message{
  61. ID: "error.dynamic",
  62. Description: "This is an unplanned error generated on the fly from details. It should be replaced",
  63. }
  64. }
  65. localizedKind := l.MustLocalize(&i18n.LocalizeConfig{
  66. DefaultMessage: &em,
  67. MessageID: em.ID,
  68. })
  69. message = l.MustLocalize(&i18n.LocalizeConfig{
  70. DefaultMessage: &i18n.Message{
  71. ID: "web.error.format",
  72. Description: "The format for errors",
  73. One: "{.Kind}: {.Detail}",
  74. LeftDelim: "{",
  75. RightDelim: "}",
  76. },
  77. TemplateData: map[string]string{
  78. "Kind": localizedKind,
  79. "Detail": detail,
  80. },
  81. PluralCount: 1,
  82. })
  83. }
  84. return Error{
  85. Kind: kind,
  86. error: errors.New(message),
  87. }
  88. }