|
@@ -2,6 +2,7 @@ package domain
|
|
|
|
|
|
import (
|
|
import (
|
|
"errors"
|
|
"errors"
|
|
|
|
+ "fmt"
|
|
|
|
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
"github.com/nicksnyder/go-i18n/v2/i18n"
|
|
)
|
|
)
|
|
@@ -40,13 +41,24 @@ type Error struct {
|
|
|
|
|
|
// MakeError creates a localized error instance, ready to print.
|
|
// MakeError creates a localized error instance, ready to print.
|
|
func MakeError(l *i18n.Localizer, kind ErrorKind, detail string) Error {
|
|
func MakeError(l *i18n.Localizer, kind ErrorKind, detail string) Error {
|
|
- format := i18n.Message{
|
|
|
|
- ID: "web.error.format",
|
|
|
|
- Description: "The format for errors",
|
|
|
|
- One: "{.Kind}: {.Detail}",
|
|
|
|
- }
|
|
|
|
var message string
|
|
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 {
|
|
if len(detail) == 0 {
|
|
message = l.MustLocalize(&i18n.LocalizeConfig{
|
|
message = l.MustLocalize(&i18n.LocalizeConfig{
|
|
MessageID: ErrorMessages[kind].ID,
|
|
MessageID: ErrorMessages[kind].ID,
|
|
@@ -55,7 +67,7 @@ func MakeError(l *i18n.Localizer, kind ErrorKind, detail string) Error {
|
|
em, ok := ErrorMessages[kind]
|
|
em, ok := ErrorMessages[kind]
|
|
if !ok {
|
|
if !ok {
|
|
em = i18n.Message{
|
|
em = i18n.Message{
|
|
- ID: "error.dynamic",
|
|
|
|
|
|
+ ID: "error.dynamic",
|
|
Description: "This is an unplanned error generated on the fly from details. It should be replaced",
|
|
Description: "This is an unplanned error generated on the fly from details. It should be replaced",
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -64,11 +76,18 @@ func MakeError(l *i18n.Localizer, kind ErrorKind, detail string) Error {
|
|
MessageID: em.ID,
|
|
MessageID: em.ID,
|
|
})
|
|
})
|
|
message = l.MustLocalize(&i18n.LocalizeConfig{
|
|
message = l.MustLocalize(&i18n.LocalizeConfig{
|
|
- DefaultMessage: &format,
|
|
|
|
|
|
+ DefaultMessage: &i18n.Message{
|
|
|
|
+ ID: "web.error.format",
|
|
|
|
+ Description: "The format for errors",
|
|
|
|
+ One: "{.Kind}: {.Detail}",
|
|
|
|
+ LeftDelim: "{",
|
|
|
|
+ RightDelim: "}",
|
|
|
|
+ },
|
|
TemplateData: map[string]string{
|
|
TemplateData: map[string]string{
|
|
"Kind": localizedKind,
|
|
"Kind": localizedKind,
|
|
"Detail": detail,
|
|
"Detail": detail,
|
|
},
|
|
},
|
|
|
|
+ PluralCount: 1,
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|