-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_config.go
51 lines (45 loc) · 1.23 KB
/
error_config.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
package goapperrors
import "errors"
// LogLevel represents log level set for an error.
// You can use LogLevel to report the level of an error to external
// services such as Sentry or Rollbar.
type LogLevel string
const (
LogLevelNone LogLevel = ""
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
LogLevelWarn LogLevel = "warning"
LogLevelError LogLevel = "error"
LogLevelFatal LogLevel = "fatal"
)
// ErrorConfig configuration of an error to be used when build error info
type ErrorConfig struct {
Status int
Code string
Title string
LogLevel LogLevel
TransKey string
Extra any
}
// GetErrorConfig gets global mapping config of an error if set
func GetErrorConfig(err error) *ErrorConfig {
if err == nil {
return nil
}
if cfg := getValueInErrorMap(err); cfg != nil {
return cfg
}
return GetErrorConfig(errors.Unwrap(err))
}
// getValueInErrorMap returns the value for the error key in the map.
// If the error key is unhashable, getting value from a map will panic.
// In that situation this func will recover from panic and return `zero` value.
func getValueInErrorMap(err error) *ErrorConfig {
defer func() {
_ = recover()
}()
if cfg, ok := mapError[err]; ok {
return cfg
}
return nil
}