diff --git a/lambda/invoke_loop.go b/lambda/invoke_loop.go index 25fc6fa4..3f679ba5 100644 --- a/lambda/invoke_loop.go +++ b/lambda/invoke_loop.go @@ -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 @@ -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) } @@ -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()) } return payload }