-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathencoder.go
66 lines (57 loc) · 2.22 KB
/
encoder.go
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
package lmdrouter
import (
"encoding/json"
"errors"
"net/http"
"github.com/aws/aws-lambda-go/events"
)
// MarshalResponse generated an events.APIGatewayProxyResponse object that can
// be directly returned via the lambda's handler function. It receives an HTTP
// status code for the response, a map of HTTP headers (can be empty or nil),
// and a value (probably a struct) representing the response body. This value
// will be marshaled to JSON (currently without base 64 encoding).
func MarshalResponse(status int, headers map[string]string, data interface{}) (
events.APIGatewayProxyResponse,
error,
) {
b, err := json.Marshal(data)
if err != nil {
status = http.StatusInternalServerError
b = []byte(`{"code":500,"message":"the server has encountered an unexpected error"}`)
}
if headers == nil {
headers = make(map[string]string)
}
headers["Content-Type"] = "application/json; charset=UTF-8"
return events.APIGatewayProxyResponse{
StatusCode: status,
IsBase64Encoded: false,
Headers: headers,
Body: string(b),
}, nil
}
// ExposeServerErrors is a boolean indicating whether the HandleError function
// should expose errors of status code 500 or above to clients. If false, the
// name of the status code is used as the error message instead.
var ExposeServerErrors = true
// HandleError generates an events.APIGatewayProxyResponse from an error value.
// If the error is an HTTPError, the response's status code will be taken from
// the error. Otherwise, the error is assumed to be 500 Internal Server Error.
// Regardless, all errors will generate a JSON response in the format
// `{ "code": 500, "error": "something failed" }`
// This format cannot currently be changed. If you do not wish to expose server
// errors (i.e. errors whose status code is 500 or above), set the
// ExposeServerErrors global variable to false.
func HandleError(err error) (events.APIGatewayProxyResponse, error) {
var httpErr HTTPError
if !errors.As(err, &httpErr) {
httpErr = HTTPError{
Code: http.StatusInternalServerError,
Message: err.Error(),
}
}
if httpErr.Code >= 500 && !ExposeServerErrors {
httpErr.Message = http.StatusText(httpErr.Code)
}
return MarshalResponse(httpErr.Code, nil, httpErr)
}