✨ Add PrintInfoLevelErrorAndParamsWithDebugTransformer error handler#410
✨ Add PrintInfoLevelErrorAndParamsWithDebugTransformer error handler#410glynternet wants to merge 2 commits into
Conversation
Generate changelog in
|
✅ Successfully generated changelog entry!Need to regenerate?Simply interact with the changelog bot comment again to regenerate these entries. 📋Changelog Preview✨ Features
|
Add a cobra error handler that prints error messages along with werror params (both safe and unsafe). When debug mode is enabled, delegates to a custom transformer function instead for detailed error output.
9333bc3 to
e5786a2
Compare
| }) | ||
| } | ||
|
|
||
| // PrintInfoLevelErrorAndParamsWithDebugTransformer provides a cobra error handler that will print the error message and params as standard. |
There was a problem hiding this comment.
as standard.
A bit confused by this wording -- does it mean as a standard part of the operation? Or to standard out?
There was a problem hiding this comment.
Also, I think it's worth mentioning in the documentation that the default implementation will print the JSON-marshaled representation of values, or the error encountered while trying to marshal the value as JSON
| _, _ = fmt.Fprintln(os.Stdout, "Error copying error buffer to file:", err.Error()) | ||
| _, _ = fmt.Fprintln(os.Stdout, "Buffer:", buf.String()) |
There was a problem hiding this comment.
Wondering if it make more sense to have the function take an optional errWriter instead (and maybe default to os.Stderr if that is nil)?
It might be a bit heavy-handed to dictate that error writing to the output stream for the command will force error output to os.Stdout
| for key := range safe { | ||
| keys = append(keys, key) | ||
| } | ||
| for key := range unsafe { | ||
| keys = append(keys, key) | ||
| } |
There was a problem hiding this comment.
nit, but I think this is a bit more readable:
| for key := range safe { | |
| keys = append(keys, key) | |
| } | |
| for key := range unsafe { | |
| keys = append(keys, key) | |
| } | |
| keys = slices.AppendSeq(keys, maps.Keys(safe)) | |
| keys = slices.AppendSeq(keys, maps.Keys(unsafe)) |
There was a problem hiding this comment.
Oh wow, yeah, this code has followed me around for years, I guess I never got around to updating to modern go std pkg APIs. Thx.
| } | ||
| } | ||
|
|
||
| func formattedParamLine(key string, val interface{}) string { |
There was a problem hiding this comment.
| func formattedParamLine(key string, val interface{}) string { | |
| func formattedParamLine(key string, val any) string { |
…Transformer
- Add errWriter io.Writer parameter (defaults to os.Stderr) instead of
writing through command.ErrOrStderr() with an os.Stdout fallback.
- Clarify godoc: drop ambiguous "as standard" wording, document the
JSON-marshalled value formatting and marshal-error fallback, and
call out that this handler does not use the cobra command's stream.
- Use maps.Keys + slices.AppendSeq for param key collection.
- Use any instead of interface{}.
Add a cobra error handler that prints error messages along with werror params (both safe and unsafe). When debug mode is enabled, delegates to a custom transformer function instead for detailed error output.
Changes Made
PrintInfoLevelErrorAndParamsWithDebugTransformer: Cobra error handler that extracts and prints werror safe/unsafe params alongside error messagesformattedParamLine: Formats individual param key-value pairs with JSON marshallinggithub.com/palantir/witchcraft-go-errorfor werror param extractionRisk Assessment
Risk Level: Low ⚡
This change is