-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_error.go
99 lines (80 loc) · 2.38 KB
/
check_error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package v2
import (
"html/template"
"strings"
"github.com/cinar/checker/v2/locales"
)
// CheckError defines the check error.
type CheckError struct {
// Code is the error code.
Code string
// data is the error data.
Data map[string]interface{}
}
const (
// DefaultLocale is the default locale.
DefaultLocale = locales.EnUS
)
// errorMessages is the map of localized error messages.
var errorMessages = map[string]map[string]string{
locales.EnUS: locales.EnUSMessages,
}
// NewCheckError creates a new check error with the given code.
func NewCheckError(code string) *CheckError {
return NewCheckErrorWithData(
code,
make(map[string]interface{}),
)
}
// NewCheckErrorWithData creates a new check error with the given code and data.
func NewCheckErrorWithData(code string, data map[string]interface{}) *CheckError {
return &CheckError{
Code: code,
Data: data,
}
}
// Error returns the error message for the check.
func (c *CheckError) Error() string {
return c.ErrorWithLocale(DefaultLocale)
}
// Is reports whether the check error is the same as the target error.
func (c *CheckError) Is(target error) bool {
if other, ok := target.(*CheckError); ok {
return c.Code == other.Code
}
return false
}
// ErrorWithLocale returns the localized error message for the check with the given locale.
func (c *CheckError) ErrorWithLocale(locale string) string {
tmpl, err := template.New("error").Parse(getLocalizedErrorMessage(locale, c.Code))
if err != nil {
return c.Code
}
var message strings.Builder
if err := tmpl.Execute(&message, c.Data); err != nil {
return c.Code
}
return message.String()
}
// RegisterLocale registers the localized error messages for the given locale.
func RegisterLocale(locale string, messages map[string]string) {
errorMessages[locale] = messages
}
// getLocalizedErrorMessage returns the localized error message for the given locale and code.
func getLocalizedErrorMessage(locale, code string) string {
if messages, found := errorMessages[locale]; found {
if message, exists := messages[code]; exists {
return message
}
}
if messages, found := errorMessages[DefaultLocale]; found {
if message, exists := messages[code]; exists {
return message
}
}
return code
}