forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrender_error.go
More file actions
88 lines (78 loc) · 3.33 KB
/
render_error.go
File metadata and controls
88 lines (78 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package rendering
import (
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/interop"
"go.amzn.com/lambda/rapi/model"
)
// RenderForbiddenWithTypeMsg method for rendering error response
func RenderForbiddenWithTypeMsg(w http.ResponseWriter, r *http.Request, errorType string, format string, args ...interface{}) {
if err := RenderJSON(http.StatusForbidden, w, r, &model.ErrorResponse{
ErrorType: errorType,
ErrorMessage: fmt.Sprintf(format, args...),
}); err != nil {
log.WithError(err).Warn("Error while rendering response")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// RenderInternalServerError method for rendering error response
func RenderInternalServerError(w http.ResponseWriter, r *http.Request) {
if err := RenderJSON(http.StatusInternalServerError, w, r, &model.ErrorResponse{
ErrorMessage: "Internal Server Error",
ErrorType: ErrorTypeInternalServerError,
}); err != nil {
log.WithError(err).Warn("Error while rendering response")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// RenderRequestEntityTooLarge method for rendering error response
func RenderRequestEntityTooLarge(w http.ResponseWriter, r *http.Request) {
if err := RenderJSON(http.StatusRequestEntityTooLarge, w, r, &model.ErrorResponse{
ErrorMessage: fmt.Sprintf("Exceeded maximum allowed payload size (%d bytes).", interop.MaxPayloadSize),
ErrorType: ErrorTypeRequestEntityTooLarge,
}); err != nil {
log.WithError(err).Warn("Error while rendering response")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// RenderTruncatedHTTPRequestError method for rendering error response
func RenderTruncatedHTTPRequestError(w http.ResponseWriter, r *http.Request) {
if err := RenderJSON(http.StatusBadRequest, w, r, &model.ErrorResponse{
ErrorMessage: "HTTP request detected as truncated",
ErrorType: ErrorTypeTruncatedHTTPRequest,
}); err != nil {
log.WithError(err).Warn("Error while rendering response")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// RenderInvalidRequestID renders invalid request ID error response
func RenderInvalidRequestID(w http.ResponseWriter, r *http.Request) {
if err := RenderJSON(http.StatusBadRequest, w, r, &model.ErrorResponse{
ErrorMessage: "Invalid request ID",
ErrorType: "InvalidRequestID",
}); err != nil {
log.WithError(err).Warn("Error while rendering response")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// RenderInvalidFunctionResponseMode renders invalid function response mode response
func RenderInvalidFunctionResponseMode(w http.ResponseWriter, r *http.Request) {
if err := RenderJSON(http.StatusBadRequest, w, r, &model.ErrorResponse{
ErrorMessage: "Invalid function response mode",
ErrorType: "InvalidFunctionResponseMode",
}); err != nil {
log.WithError(err).Warn("Error while rendering response")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// RenderInteropError is a convenience method for interpreting interop errors
func RenderInteropError(writer http.ResponseWriter, request *http.Request, err error) {
if err == interop.ErrInvalidInvokeID || err == interop.ErrResponseSent {
RenderInvalidRequestID(writer, request)
} else {
log.Panic(err)
}
}