Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 07844a8

Browse files
authored
Merge pull request #13 from stesie/fix-status-code-propagation
Correctly propagate statusCode (of API-Gateway Event Response)
2 parents d076e2a + 6922429 commit 07844a8

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type message struct {
3838

3939
type responseWrapper struct {
4040
http.ResponseWriter
41+
StatusCode int
4142
Body []byte
4243
}
4344

@@ -71,6 +72,10 @@ func (rw *responseWrapper) Write(data []byte) (int, error) {
7172
return len(data), nil
7273
}
7374

75+
func (rw *responseWrapper) WriteHeader(statusCode int) {
76+
rw.StatusCode = statusCode;
77+
}
78+
7479
func setupEnv() error {
7580
environment["_HANDLER"], _ = os.LookupEnv("_HANDLER")
7681
environment["LAMBDA_TASK_ROOT"], _ = os.LookupEnv("LAMBDA_TASK_ROOT")
@@ -206,12 +211,12 @@ func responseHandler(w http.ResponseWriter, r *http.Request) {
206211
func mapEvent(h http.Handler) http.Handler {
207212
eventType, _ := os.LookupEnv("EVENT")
208213
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
209-
rw := responseWrapper{w, []byte{}}
214+
rw := responseWrapper{w, 200, []byte{}}
210215
switch eventType {
211216
case "API_GATEWAY":
212217
apiGateway.Request(r)
213218
h.ServeHTTP(&rw, r)
214-
apiGateway.Response(w, rw.Body)
219+
apiGateway.Response(w, rw.StatusCode, rw.Body)
215220
default:
216221
h.ServeHTTP(w, r)
217222
}

pkg/events/apiGateway/mapper.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,19 @@ func Request(r *http.Request) {
4040
r.Body = ioutil.NopCloser(bytes.NewBuffer(js))
4141
}
4242

43-
func Response(w http.ResponseWriter, data []byte) (int, error) {
43+
func Response(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
4444
var js events.APIGatewayProxyResponse
4545
if err := json.Unmarshal(data, &js); err != nil {
4646
return 0, err
4747
}
4848
for k, v := range js.Headers {
4949
w.Header().Set(k, v)
5050
}
51-
w.WriteHeader(js.StatusCode)
51+
52+
if js.StatusCode >= 200 {
53+
statusCode = js.StatusCode
54+
}
55+
w.WriteHeader(statusCode)
56+
5257
return w.Write([]byte(js.Body))
5358
}

0 commit comments

Comments
 (0)