-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
94 lines (81 loc) · 3.13 KB
/
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
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
package goapperrors
import (
"net/http"
)
// Config to provide global config for the library
type Config struct {
// Debug flag indicates debug mode (default: `false`).
// If `false`, app error `debug` string can't be set.
Debug bool
// WrapFunc to wrap an error with adding stack trace (default: `nil`).
// This function is nil by default which means the library will use default value
// which is function Wrap from `github.com/go-errors/errors`.
WrapFunc func(error) error
// MaxStackDepth max stack depth (default: `50`).
// If WrapFunc is set with custom value, this config has no effect.
MaxStackDepth int
// DefaultLanguage default language (default: `LanguageEn`)
DefaultLanguage Language
// TranslationFunc function to translate message into a specific language (default: `nil`)
TranslationFunc TranslationFunc
// FallbackToErrorContentOnMissingTranslation indicates fallback to error content
// when translation failed (default: `true`).
// If `false`, when translation fails, the output message will be empty.
FallbackToErrorContentOnMissingTranslation bool
// MultiErrorSeparator separator of multiple error strings (default: `\n`)
MultiErrorSeparator string
// DefaultErrorStatus default status for error if unset (default: `500`)
DefaultErrorStatus int
// DefaultValidationErrorStatus default status for validation error if unset (default: `400`)
DefaultValidationErrorStatus int
// DefaultValidationErrorCode default code for validation error if unset (default: `ErrValidation`)
DefaultValidationErrorCode string
// DefaultLogLevel default log level for errors if unset (default: `LogLevelNone`)
DefaultLogLevel LogLevel
}
func (cfg *Config) setDefault() {
if cfg.MaxStackDepth == 0 {
cfg.MaxStackDepth = defaultMaxStackDepth
}
if cfg.DefaultLanguage == nil {
cfg.DefaultLanguage = defaultLanguage
}
if cfg.MultiErrorSeparator == "" {
cfg.MultiErrorSeparator = defaultErrorSeparator
}
if cfg.DefaultErrorStatus == 0 {
cfg.DefaultErrorStatus = defaultErrorStatus
}
if cfg.DefaultValidationErrorStatus == 0 {
cfg.DefaultValidationErrorStatus = defaultValidationErrorStatus
}
if cfg.DefaultValidationErrorCode == "" {
cfg.DefaultValidationErrorCode = defaultValidationErrorCode
}
if cfg.DefaultLogLevel == LogLevelNone {
cfg.DefaultLogLevel = defaultLogLevel
}
}
const (
defaultMaxStackDepth = 50
defaultLanguage = LanguageEn
defaultErrorSeparator = "\n"
defaultErrorStatus = http.StatusInternalServerError
defaultValidationErrorStatus = http.StatusBadRequest
defaultValidationErrorCode = "ErrValidation"
defaultLogLevel = LogLevelNone
)
var (
globalConfig = &Config{
Debug: false,
MaxStackDepth: defaultMaxStackDepth,
DefaultLanguage: defaultLanguage,
FallbackToErrorContentOnMissingTranslation: true,
MultiErrorSeparator: defaultErrorSeparator,
DefaultErrorStatus: defaultErrorStatus,
DefaultValidationErrorStatus: defaultValidationErrorStatus,
DefaultValidationErrorCode: defaultValidationErrorCode,
DefaultLogLevel: defaultLogLevel,
}
mapError = make(map[error]*ErrorConfig, 50) //nolint:mnd
)