-
Notifications
You must be signed in to change notification settings - Fork 38
K8SPS-296: improve errors logging #1238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package util | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // WrapWithDeepestStack preserves the full error message chain, but limits | ||
| // verbose logging to the deepest pkg/errors stack. This avoids the default | ||
| // github.com/pkg/errors behavior where %+v prints stack traces from all wraps. | ||
| func WrapWithDeepestStack(err error, msg string) error { | ||
| if err == nil { | ||
| return nil | ||
| } | ||
|
|
||
| stackErr := err | ||
| for next := errors.Unwrap(err); next != nil; next = errors.Unwrap(next) { | ||
| if _, ok := next.(stackTracer); ok { | ||
| stackErr = next | ||
| } | ||
| } | ||
|
|
||
| text := err.Error() | ||
| if msg != "" { | ||
| text = msg + ": " + text | ||
| } | ||
|
|
||
| return &deepStackErr{ | ||
| msg: text, | ||
| cause: stackErr, | ||
| } | ||
|
Comment on lines
+18
to
+33
|
||
| } | ||
|
|
||
| type deepStackErr struct { | ||
| msg string | ||
| cause error | ||
| } | ||
|
|
||
| func (e *deepStackErr) Error() string { return e.msg } | ||
|
|
||
| func (e *deepStackErr) Cause() error { return e.cause } | ||
|
|
||
| func (e *deepStackErr) Unwrap() error { return e.cause } | ||
|
|
||
| func (e *deepStackErr) Format(s fmt.State, verb rune) { | ||
| switch verb { | ||
| case 'v': | ||
| if s.Flag('+') { | ||
| _, _ = io.WriteString(s, e.msg) | ||
| if e.cause != nil { | ||
| _, _ = io.WriteString(s, "\n") | ||
| _, _ = fmt.Fprintf(s, "%+v", e.cause) | ||
| } | ||
| return | ||
| } | ||
| fallthrough | ||
| case 's': | ||
| _, _ = io.WriteString(s, e.msg) | ||
| case 'q': | ||
| _, _ = fmt.Fprintf(s, "%q", e.msg) | ||
| } | ||
| } | ||
|
|
||
| type stackTracer interface { | ||
| StackTrace() errors.StackTrace | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isTruncatedJSONErr relies on a strict string equality check against "unexpected end of JSON input". This is brittle if the JSON package changes wording or if the error gets wrapped with additional context. Prefer detecting this via errors.As to *json.SyntaxError (and/or using strings.Contains on the message) so the check is resilient to wrapping/prefixes.