Skip to content

Commit

Permalink
fix xrayException shape - the json tags differ from InvokeResponse_Error
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoffatt committed Jan 24, 2024
1 parent 55f2e0a commit 1aaad14
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
29 changes: 19 additions & 10 deletions lambda/invoke_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ func handleInvoke(invoke *invoke, handler *handlerOptions) error {
}

func reportFailure(invoke *invoke, invokeErr *messages.InvokeResponse_Error) error {
errorForXRay := makeErrorForXRay(invokeErr)
errorPayload := errorForXRay.Exceptions[0]
errorPayload := safeMarshal(invokeErr)
log.Printf("%s", errorPayload)

causeForXRay, err := json.Marshal(errorForXRay)
causeForXRay, err := json.Marshal(makeXRayError(invokeErr))
if err != nil {
return fmt.Errorf("unexpected error occured when serializing the function error cause for X-Ray: %v", err)
}
Expand Down Expand Up @@ -174,13 +173,19 @@ func safeMarshal(v interface{}) []byte {
return payload
}

type errorForXRay struct {
WorkingDirectory string `json:"working_directory"`
Exceptions []json.RawMessage `json:"exceptions"` // returned as bytes to avoid double-serializing
Paths []string `json:"paths"`
type xrayException struct {
Type string `json:"type"`
Message string `json:"message"`
Stack []*messages.InvokeResponse_Error_StackFrame `json:"stack"`
}

func makeErrorForXRay(invokeResponseError *messages.InvokeResponse_Error) *errorForXRay {
type xrayError struct {
WorkingDirectory string `json:"working_directory"`
Exceptions []xrayException `json:"exceptions"`
Paths []string `json:"paths"`
}

func makeXRayError(invokeResponseError *messages.InvokeResponse_Error) *xrayError {
pathSet := make(map[string]struct{}, len(invokeResponseError.StackTrace))
for _, frame := range invokeResponseError.StackTrace {
pathSet[frame.Path] = struct{}{}
Expand All @@ -190,9 +195,13 @@ func makeErrorForXRay(invokeResponseError *messages.InvokeResponse_Error) *error
paths = append(paths, path)
}
cwd, _ := os.Getwd()
return &errorForXRay{
return &xrayError{
WorkingDirectory: cwd,
Paths: paths,
Exceptions: []json.RawMessage{safeMarshal(invokeResponseError)},
Exceptions: []xrayException{{
Type: invokeResponseError.Type,
Message: invokeResponseError.Message,
Stack: invokeResponseError.StackTrace,
}},
}
}
16 changes: 9 additions & 7 deletions lambda/invoke_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,25 @@ func TestCustomErrorMarshaling(t *testing.T) {
func TestXRayCausePlumbing(t *testing.T) {
errors := []error{
messages.InvokeResponse_Error{
Type: "yolo",
Message: "hello",
Type: "yoloError",
Message: "hello yolo",
StackTrace: []*messages.InvokeResponse_Error_StackFrame{
{Label: "yolo", Path: "yolo", Line: 2},
{Label: "hi", Path: "hello/hello", Line: 12},
},
},
}
wd, _ := os.Getwd()
expected := []string{
`{
"working_directory":"` + wd + `",
"paths": ["yolo"],
"paths": ["yolo", "hello/hello"],
"exceptions": [{
"errorType": "yolo",
"errorMessage": "hello",
"stackTrace": [
{"label": "yolo", "path": "yolo", "line": 2}
"type": "yoloError",
"message": "hello yolo",
"stack": [
{"label": "yolo", "path": "yolo", "line": 2},
{"label": "hi", "path": "hello/hello", "line": 12}
]
}]
}`,
Expand Down

0 comments on commit 1aaad14

Please sign in to comment.