forked from golang/glog
-
Notifications
You must be signed in to change notification settings - Fork 227
WIP: structured error, funcr.PseudoStruct #361
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
Open
pohly
wants to merge
2
commits into
kubernetes:main
Choose a base branch
from
pohly:structured-error-pseudo-struct
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+346
−9
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,4 @@ module k8s.io/klog/v2 | |
|
|
||
| go 1.21 | ||
|
|
||
| require github.com/go-logr/logr v1.4.1 | ||
| require github.com/go-logr/logr v1.4.3 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= | ||
| github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
| github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= | ||
| github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package klog | ||
|
|
||
| import "slices" | ||
|
|
||
| // ErrorDetailer provides additional information about an error. | ||
| // When an error value implements this additional interface, | ||
| // the result of ErrorDetails will be logged in a separate key/value | ||
| // pair. The result of Error is logged as usual. | ||
| // | ||
| // In Kubernetes, text and JSON output backends (aka klog and zapr) | ||
| // will support this with "<error key>Details" (typically "errDetails") | ||
| // as key for the additional value. | ||
| // | ||
| // Other backends might not support this, so all relevant information | ||
| // should be in the error string. | ||
| type ErrorDetailer interface { | ||
| ErrorDetails() any | ||
| } | ||
|
|
||
| // ErrorWithDetails adds additional details to an error for logging. | ||
| // If the base error already has such additional details, they | ||
| // will be included in a list of details. | ||
| // | ||
| // A [PseudoStruct] can be used to log some key/value pairs as | ||
| // if they were in a struct, without having to define such a struct. | ||
| // The formatting may be nicer, too. | ||
| func ErrorWithDetails(err error, details any) error { | ||
| // This could be implemented as ErrorWithDetailsFunc(err, func() { return details }), | ||
| // but having the details visible in the error instance may be more useful for | ||
| // interactive debugging. | ||
| return &errWithDetails{err, details} | ||
| } | ||
|
|
||
| type errWithDetails struct { | ||
| error | ||
| details any | ||
| } | ||
|
|
||
| var _ error = &errWithDetails{} | ||
| var _ ErrorDetailer = &errWithDetails{} | ||
|
|
||
| func (err *errWithDetails) ErrorDetails() any { | ||
| if base, ok := err.error.(ErrorDetailer); ok { | ||
| baseDetails := base.ErrorDetails() | ||
| if baseDetailsList, ok := baseDetails.([]any); ok { | ||
| // Flatten the list. | ||
| return append(slices.Clone(baseDetailsList), err.details) | ||
| } | ||
| // Use a pair of values in a slice which gets detected above when nesting multiple times. | ||
| return []any{baseDetails, err.details} | ||
| } | ||
| return err.details | ||
| } | ||
|
|
||
| // ErrorWithDetailsFunc adds additional details to an error for logging. | ||
| // In contrast to [ErrorWithDetails], the additional details are provided | ||
| // by the given function, which will be called only when needed. This | ||
| // can be used to avoid building some potentially expensive data structure | ||
| // that will not be needed when the error does not get logged. | ||
| // | ||
| // If the base error already has such additional details, they | ||
| // will be included in a list of details. | ||
| // | ||
| // A [PseudoStruct] can be used to log some key/value pairs as | ||
| // if they were in a struct, without having to define such a struct. | ||
| // The formatting may be nicer, too. | ||
| func ErrorWithDetailsFunc(err error, details func() any) error { | ||
| return &errWithDetailsFunc{err, details} | ||
| } | ||
|
|
||
| type errWithDetailsFunc struct { | ||
| error | ||
| details func() any | ||
| } | ||
|
|
||
| var _ error = &errWithDetailsFunc{} | ||
| var _ ErrorDetailer = &errWithDetailsFunc{} | ||
|
|
||
| func (err *errWithDetailsFunc) ErrorDetails() any { | ||
| if base, ok := err.error.(ErrorDetailer); ok { | ||
| baseDetails := base.ErrorDetails() | ||
| if baseDetailsList, ok := baseDetails.([]any); ok { | ||
| // Flatten the list. | ||
| return append(slices.Clone(baseDetailsList), err.details()) | ||
| } | ||
| // Use a pair of values in a slice which gets detected above when nesting multiple times. | ||
| return []any{baseDetails, err.details()} | ||
| } | ||
| return err.details() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| Copyright The Kubernetes Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package klog | ||
|
|
||
| import ( | ||
| "errors" | ||
| "reflect" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestErrorDetails(t *testing.T) { | ||
| base := errors.New("base") | ||
|
|
||
| for name, tc := range map[string]struct { | ||
| err error | ||
| expectErrorString string | ||
| expectErrorDetails any | ||
| }{ | ||
| "simple": {ErrorWithDetails(base, 42), "base", 42}, | ||
| "pair": {ErrorWithDetails(ErrorWithDetails(base, "hello"), "world"), "base", []any{"hello", "world"}}, | ||
| "nested": {ErrorWithDetails(ErrorWithDetails(ErrorWithDetails(base, "hello"), "world"), "thanks"), "base", []any{"hello", "world", "thanks"}}, | ||
|
|
||
| "simple-func": {ErrorWithDetailsFunc(base, func() any { return 42 }), "base", 42}, | ||
| "pair-func": {ErrorWithDetailsFunc(ErrorWithDetails(base, "hello"), func() any { return "world" }), "base", []any{"hello", "world"}}, | ||
| "nested-func": {ErrorWithDetailsFunc(ErrorWithDetails(ErrorWithDetails(base, "hello"), "world"), func() any { return "thanks" }), "base", []any{"hello", "world", "thanks"}}, | ||
| } { | ||
| t.Run(name, func(t *testing.T) { | ||
| if actual, expect := tc.err.Error(), tc.expectErrorString; actual != expect { | ||
| t.Errorf("expected error string %q, got %q", expect, actual) | ||
| } | ||
| if actual, expect := tc.err.(ErrorDetailer).ErrorDetails(), tc.expectErrorDetails; !reflect.DeepEqual(actual, expect) { | ||
| t.Errorf("expected error details %#v, got %#v", expect, actual) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This could become a type alias for
logr.ErrorDetailer. It probably makes sense to decide whether we want such a type there before merging this and the zapr PR.I'll prepare a logr PR for discussion.
/cc @thockin