Skip to content
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

Ensure context.DeadlineExceeded is preserved through the network. #89

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions errbase/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func decodeErrorString(_ context.Context, msg string, _ []string, _ proto.Messag
return goErr.New(msg)
}

// context.DeadlineExceeded uses a custom type.
func decodeDeadlineExceeded(_ context.Context, _ string, _ []string, _ proto.Message) error {
return context.DeadlineExceeded
}

// errors.fundamental from github.com/pkg/errors cannot be encoded
// exactly because it includes a non-serializable stack trace
// object. In order to work with it, we encode it by dumping
Expand Down Expand Up @@ -183,6 +188,8 @@ func init() {
baseErr := goErr.New("")
RegisterLeafDecoder(GetTypeKey(baseErr), decodeErrorString)

RegisterLeafDecoder(GetTypeKey(context.DeadlineExceeded), decodeDeadlineExceeded)

pkgE := pkgErr.New("")
RegisterLeafEncoder(GetTypeKey(pkgE), encodePkgFundamental)

Expand Down
8 changes: 8 additions & 0 deletions errbase/adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ func TestAdaptProtoErrorsWithWrapper(t *testing.T) {
tt.CheckDeepEqual(newErr, origErr)
}

func TestAdaptContextCanceled(t *testing.T) {
// context.DeadlineExceeded is preserved exactly.

tt := testutils.T{T: t}
newErr := network(t, context.DeadlineExceeded)
tt.CheckEqual(newErr, context.DeadlineExceeded)
}

func TestAdaptOsErrors(t *testing.T) {
// The special os error types are preserved exactly.

Expand Down
34 changes: 9 additions & 25 deletions fmttests/testdata/format/leaves-via-network
Original file line number Diff line number Diff line change
Expand Up @@ -232,44 +232,28 @@ ctx-deadline oneline twoline
opaque
accept %\+v via Formattable.*IRREGULAR: not same as %\+v
----
&errbase.opaqueLeaf{
msg: "context deadline exceeded",
details: errorspb.EncodedErrorDetails{
OriginalTypeName: "context/context.deadlineExceededError",
ErrorTypeMark: errorspb.ErrorTypeMark{FamilyName:"context/context.deadlineExceededError", Extension:""},
ReportablePayload: nil,
FullDetails: (*types.Any)(nil),
},
}
context.deadlineExceededError{}
=====
===== non-redactable formats
=====
== %#v
&errbase.opaqueLeaf{
msg: "context deadline exceeded",
details: errorspb.EncodedErrorDetails{
OriginalTypeName: "context/context.deadlineExceededError",
ErrorTypeMark: errorspb.ErrorTypeMark{FamilyName:"context/context.deadlineExceededError", Extension:""},
ReportablePayload: nil,
FullDetails: (*types.Any)(nil),
},
}
context.deadlineExceededError{}
== Error()
context deadline exceeded
== %v = Error(), good
== %s = Error(), good
== %q = quoted Error(), good
== %x = hex Error(), good
== %X = HEX Error(), good
== %+v
context deadline exceeded
(1) context deadline exceeded
Error types: (1) *errbase.opaqueLeaf
== %+v = Error(), ok
== %#v via Formattable() = %#v, good
== %v via Formattable() = Error(), good
== %s via Formattable() = %v via Formattable(), good
== %q via Formattable() = quoted %v via Formattable(), good
== %+v via Formattable() == %+v, good
== %+v via Formattable() (IRREGULAR: not same as %+v)
context deadline exceeded
(1) context deadline exceeded
Error types: (1) context.deadlineExceededError
=====
===== redactable formats
=====
Expand All @@ -280,10 +264,10 @@ context deadline exceeded
== printed via redact Printf() %q, refused - good
== printed via redact Printf() %x, refused - good
== printed via redact Printf() %X, refused - good
== printed via redact Printf() %+v, ok - congruent with %+v
== printed via redact Printf() %+v, ok - congruent with %+v via Formattable()
context deadline exceeded
(1) context deadline exceeded
Error types: (1) *errbase.opaqueLeaf
Error types: (1) context.deadlineExceededError
=====
===== Sentry reporting
=====
Expand Down