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

Marshal error with full context chain #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nickng
Copy link
Member

@nickng nickng commented Nov 29, 2022

When using the new Augment function to add context and wrap errors, the error message in Go is chained and displayed with the full chain of context:

err := InternalService("code", "message", nil)
augmentedErr := Augment(err, "context", nil)

// This is "internal_service.code: context: message"
augmentedErr.Error()

But the marshaled error only displays the first level of context, i.e.

// This is just "context"
Marshal(augmentedErr.(*Error)).Message

This commit changes the Marshal behaviour to include the full context chain of error, so it's actually useful outside of Go when marshaled.

// This is now "context: message"
Marshal(augmentedErr.(*Error)).Message

@@ -235,6 +291,42 @@ var unmarshalTestCases = []struct {
Retryable: nil,
},
},
// Wrapped errors only gets unmarshaled as a single error
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marshaling and unmarshaling are asymmetric to start with, but the error code is preserved.

// these are separate from above because the marshaling and unmarshaling isn't symmetric.

@nickng nickng marked this pull request as ready for review November 29, 2022 12:18
When using the new `Augment` function to add context and wrap errors,
the error message in Go is chained and displayed with the full chain
of context:

```
err := InternalService("code", "message", nil)
augmentedErr := Augment(err, "context", nil)

// This is "internal_service.code: context: message"
augmentedErr.Error()
```

But the marshaled error only displays the first level of context,
i.e.

```
// This is just "context"
Marshal(augmentedErr.(*Error)).Message
```

This commit changes the Marshal behaviour to include the full context
chain of error, so it's actually useful outside of Go when marshaled.

```
// This is now "context: message"
Marshal(augmentedErr.(*Error)).Message
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants