Skip to content

Commit 04c5e18

Browse files
authored
Add docs of configuration (#16)
1 parent 5cadb58 commit 04c5e18

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

README.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func main() {
3131
gae.Init(&gae.Config{
3232
Debug: ENV == "development",
3333
DefaultLogLevel: gae.LogLevelInfo,
34-
TranslationFunc: func (gae.Language, key string, params map[string]any) {
34+
TranslationFunc: func (lang gae.Language, key string, params map[string]any) {
3535
// Provides your implementation to translate message
3636
},
3737
})
@@ -107,18 +107,17 @@ if `user.ID` is not in `project.userIDs` {
107107
// `adapter` code to transform the validation errors to `AppError`s.
108108

109109
// Add a new file to the above directory, says `apperrors/validation_errors.go`.
110-
// This adapter function will be used later to create validation error.
111-
func ValidationErrorInfoBuilder(appErr AppError, buildCfg *gae.InfoBuilderConfig) *gae.InfoBuilderResult {
112-
// Extracts the inner validation error
110+
// This building function will be used later to create validation error.
111+
func ValidationErrorInfoBuilder(err AppError, buildCfg *gae.InfoBuilderConfig) *gae.InfoBuilderResult {
112+
// Extracts the inner error and casts it to the validation error type you use
113113
vldErr := &thirdPartyLib.Error{}
114-
if !errors.As(appErr, &vldErr) {
115-
// panic, should not happen
114+
if !errors.As(err, &vldErr) {
115+
// panic, this should not happen
116116
}
117-
118117
return &gae.InfoBuilderResult{
119118
// Transform the error from the 3rd party lib to ErrorInfo struct
120119
ErrorInfo: &gae.ErrorInfo{
121-
Message: buildCfg.TranslationFunc(buildCfg.Language, vldErr.getMessage(), appErr.Params()),
120+
Message: buildCfg.TranslationFunc(buildCfg.Language, vldErr.getMessage(), err.Params()),
122121
Source: vldErr.getSource(),
123122
...
124123
}
@@ -171,9 +170,48 @@ func (h ProjectHandler) UpdateProject() {
171170
}
172171
```
173172

174-
### Configuration
173+
### Global configuration
174+
175+
[See the full code](config.go)
176+
177+
#### Debug (default: `false`)
178+
179+
You should synchronize this option with the ENV value. If it is `true`, error building will
180+
return the fields `Cause` and `Debug` which is convenient for development. If it is `false`,
181+
they will not be returned which is more secured in `production` env as their values can
182+
have sensitive information.
183+
184+
#### WrapFunc (default: `nil`)
185+
186+
If this value is default (which is `nil`), `go-apperrors` will use the lib github.com/go-errors/errors
187+
to wrap and attach stack trace to errors. If you don't want to attach stack trace, just provide an
188+
self-implementation version on initialization.
189+
190+
```go
191+
Init(&Config{
192+
WrapFunc: func (err error) error { return err }
193+
})
194+
```
195+
196+
#### TranslationFunc (default: `nil`)
197+
198+
Sets this option by providing a function to help the lib translate error messages. Otherwise,
199+
the translation will be disabled.
200+
201+
```go
202+
Init(&Config{
203+
TranslationFunc: func (lang Language, key string, params map[string]any) {
204+
// Provides your implementation to translate message
205+
},
206+
})
207+
```
208+
209+
#### FallbackToErrorContentOnMissingTranslation (default: `true`)
210+
211+
When translation fails, if this flag is `true`, the error content will be used to assign to
212+
the output `Message` field. Otherwise, the output message will be empty.
175213

176-
TBD
214+
NOTE: turn off this flag if you don't want to reveal sensitive information on building.
177215

178216
## Contributing
179217

0 commit comments

Comments
 (0)