Skip to content

Commit

Permalink
Merge pull request #89 from knz/20220118-ctx-deadline
Browse files Browse the repository at this point in the history
  • Loading branch information
knz authored Jan 18, 2022
2 parents e8b8327 + 9d7d1ef commit 08022dc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 25 deletions.
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

0 comments on commit 08022dc

Please sign in to comment.