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
Open
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
24 changes: 21 additions & 3 deletions marshaling.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package terrors

import (
"strings"

pe "github.com/monzo/terrors/proto"
"github.com/monzo/terrors/stack"
)
Expand All @@ -15,22 +17,38 @@ func Marshal(e *Error) *pe.Error {
}
}

// Build message with all the context
errMessage := strings.Builder{}
errMessage.WriteString(e.Message)
next := e.cause
for next != nil {
errMessage.WriteString(": ")
switch typed := next.(type) {
case *Error:
errMessage.WriteString(typed.Message)
next = typed.cause
case error:
errMessage.WriteString(typed.Error())
next = nil
}
}

retryable := &pe.BoolValue{}
if e.IsRetryable != nil {
retryable.Value = *e.IsRetryable
}

err := &pe.Error{
err := pe.Error{
Code: e.Code,
Message: e.Message,
Message: errMessage.String(),
rubendura marked this conversation as resolved.
Show resolved Hide resolved
Stack: stackToProto(e.StackFrames),
Params: e.Params,
Retryable: retryable,
}
if err.Code == "" {
err.Code = ErrUnknown
}
return err
return &err
}

// Unmarshal a protobuf error into a local error
Expand Down
92 changes: 92 additions & 0 deletions marshaling_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package terrors

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -122,6 +123,61 @@ var marshalTestCases = []struct {
Retryable: nil,
},
},
// Wrapped errors
{
Augment(&Error{
Code: ErrInternalService,
Message: "bar",
}, "foo", nil).(*Error),
&pe.Error{
Code: ErrInternalService,
Message: "foo: bar",
Retryable: nil,
Params: map[string]string{},
},
},
{
Augment(&Error{
Code: ErrInternalService,
Message: "bar",
}, "foo", map[string]string{"key": "value"}).(*Error),
&pe.Error{
Code: ErrInternalService,
Message: "foo: bar",
Retryable: nil,
Params: map[string]string{"key": "value"},
},
},
{
// Nested Augment
Augment(
Augment(&Error{
Code: ErrInternalService,
Message: "baz",
},
"bar",
map[string]string{"key": "value"},
),
"foo",
map[string]string{"key2": "value2"},
).(*Error),
&pe.Error{
Code: ErrInternalService,
Message: "foo: bar: baz",
Retryable: nil,
Params: map[string]string{"key": "value", "key2": "value2"},
},
},
{
// Wrapping a Go error
Augment(fmt.Errorf("a go error"), "boom", map[string]string{"key": "value"}).(*Error),
&pe.Error{
Code: ErrInternalService,
Message: "boom: a go error",
Retryable: nil,
Params: map[string]string{"key": "value"},
},
},
}

func TestMarshal(t *testing.T) {
Expand Down Expand Up @@ -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.

{
&Error{
Code: ErrInternalService,
Message: "foo: bar: baz", // Augment(Augment(bazErr, "bar", nil), "foo", nil)
Params: map[string]string{},
},
&pe.Error{
Code: ErrInternalService,
Message: "foo: bar: baz",
Retryable: &pe.BoolValue{
Value: false,
},
},
},
{
&Error{
Code: ErrInternalService,
Message: "foo: bar",
Params: map[string]string{
"key": "value",
"key2": "value2",
},
},
&pe.Error{
Code: ErrInternalService,
Message: "foo: bar",
Retryable: &pe.BoolValue{
Value: false,
},
Params: map[string]string{
"key": "value",
"key2": "value2",
},
},
},
}

func TestUnmarshal(t *testing.T) {
Expand Down