Skip to content

safeMarshal should escape the error message #327

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

Merged
merged 3 commits into from
Oct 3, 2020
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
15 changes: 11 additions & 4 deletions lambda/invoke_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (
)

const (
serializationErrorFormat = `{"errorType": "Runtime.SerializationError", "errorMessage": "%s"}`
msPerS = int64(time.Second / time.Millisecond)
nsPerMS = int64(time.Millisecond / time.Nanosecond)
msPerS = int64(time.Second / time.Millisecond)
nsPerMS = int64(time.Millisecond / time.Nanosecond)
)

// startRuntimeAPILoop will return an error if handling a particular invoke resulted in a non-recoverable error
Expand Down Expand Up @@ -103,7 +102,15 @@ func convertInvokeRequest(invoke *invoke) (*messages.InvokeRequest, error) {
func safeMarshal(v interface{}) []byte {
payload, err := json.Marshal(v)
if err != nil {
return []byte(fmt.Sprintf(serializationErrorFormat, err.Error()))
v := &messages.InvokeResponse_Error{
Type: "Runtime.SerializationError",
Message: err.Error(),
}
payload, err := json.Marshal(v)
if err != nil {
panic(err) // never reach
}
return payload
}
return payload
}
12 changes: 12 additions & 0 deletions lambda/invoke_loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ func TestReadPayload(t *testing.T) {

}

type invalidPayload struct{}

func (invalidPayload) MarshalJSON() ([]byte, error) {
return nil, errors.New(`some error that contains '"'`)
}

func TestSafeMarshal_SerializationError(t *testing.T) {
payload := safeMarshal(invalidPayload{})
want := `{"errorMessage":"json: error calling MarshalJSON for type lambda.invalidPayload: some error that contains '\"'","errorType":"Runtime.SerializationError"}`
assert.Equal(t, want, string(payload))
}

type requestRecord struct {
nGets int
nPosts int
Expand Down