Skip to content
Closed
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
14 changes: 8 additions & 6 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 @@ -48,7 +47,7 @@ func handleInvoke(invoke *invoke, function *Function) error {
}

if functionResponse.Error != nil {
payload := safeMarshal(functionResponse.Error)
payload := mustMarshal(functionResponse.Error)
if err := invoke.failure(payload, contentTypeJSON); err != nil {
return fmt.Errorf("unexpected error occured when sending the function error to the API: %v", err)
}
Expand Down Expand Up @@ -100,10 +99,13 @@ func convertInvokeRequest(invoke *invoke) (*messages.InvokeRequest, error) {
return res, nil
}

func safeMarshal(v interface{}) []byte {
// mustMarshal is like json.Marshal but panics if the v cannot be marshaled.
// use this if you know the v doesn't contain values that json.Marshal can't marshal,
// such as Channel, complex, and function values.
func mustMarshal(v interface{}) []byte {
payload, err := json.Marshal(v)
if err != nil {
return []byte(fmt.Sprintf(serializationErrorFormat, err.Error()))
panic("lambda: " + err.Error())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I've misunderstood.
json.Marshal(v) doesn't return any error, because we know *messages.InvokeResponse_Error can be marshaled.
So, we don't need to treat this case, and just enough to panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
return payload
}