Added headers to error response

This commit is contained in:
mitchelljfs 2018-08-30 13:07:44 -07:00
parent 4399e41cd2
commit ade833df39
2 changed files with 8 additions and 2 deletions

View File

@ -138,6 +138,7 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
response.StatusCode = http.StatusNotFound response.StatusCode = http.StatusNotFound
response.Body = string(respbody) response.Body = string(respbody)
response.Headers = r.headers
return response return response
} }
@ -162,13 +163,14 @@ func (r *APIGRouter) Respond() events.APIGatewayProxyResponse {
respbody, _ := json.Marshal(map[string]string{"error": err.Error()}) respbody, _ := json.Marshal(map[string]string{"error": err.Error()})
if strings.Contains(err.Error(), "record not found") { if strings.Contains(err.Error(), "record not found") {
status = 204 status = 204
} else if status < 400 { } else if status != 204 && status < 400 {
status = 400 status = 400
} }
log.Printf("%v error: %v", status, err.Error()) log.Printf("%v error: %v", status, err.Error())
response.StatusCode = status response.StatusCode = status
response.Body = string(respbody) response.Body = string(respbody)
response.Headers = r.headers
return response return response
} }
} }

View File

@ -70,6 +70,10 @@ func TestRouterSpec(t *testing.T) {
So(response.StatusCode, ShouldEqual, http.StatusNotFound) So(response.StatusCode, ShouldEqual, http.StatusNotFound)
So(response.Body, ShouldEqual, "{\"error\":\"no route matching path found\"}") So(response.Body, ShouldEqual, "{\"error\":\"no route matching path found\"}")
So(response.Headers, ShouldResemble, map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true",
})
}) })
}) })
@ -112,7 +116,7 @@ func TestRouterSpec(t *testing.T) {
Convey("When the handler func does return a record not found", func() { Convey("When the handler func does return a record not found", func() {
hdlrfunc := func(ctx *APIGContext) { hdlrfunc := func(ctx *APIGContext) {
ctx.Status = http.StatusBadRequest ctx.Status = http.StatusNoContent
ctx.Body = []byte("hello") ctx.Body = []byte("hello")
ctx.Err = errors.New("record not found") ctx.Err = errors.New("record not found")