From cfa6dc77d8dff5cbf916192da09f73830df3f7ea Mon Sep 17 00:00:00 2001 From: Janne Snabb Date: Thu, 7 Feb 2019 21:39:01 +0200 Subject: [PATCH] Use InternalServerError (500) instead of GatewayTimeoutError (504). Gateway Timeout Error is not really correct error status code. It is returned in cases when the request or response conversion fails. Thus Internal Server Error feels more appropriate. --- chi/adapter.go | 4 ++-- core/types.go | 7 ++++++- gin/adapter.go | 4 ++-- gorillamux/adapter.go | 4 ++-- handlerfunc/adapter.go | 4 ++-- httpadapter/adapter.go | 4 ++-- negroni/adapter.go | 4 ++-- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/chi/adapter.go b/chi/adapter.go index e886960..b1c9cd8 100644 --- a/chi/adapter.go +++ b/chi/adapter.go @@ -34,7 +34,7 @@ func (g *ChiLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayP chiRequest, err := g.ProxyEventToHTTPRequest(req) if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err) + return core.InternalServerError(), core.NewLoggedError("Could not convert proxy event to request: %v", err) } respWriter := core.NewProxyResponseWriter() @@ -42,7 +42,7 @@ func (g *ChiLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayP proxyResponse, err := respWriter.GetProxyResponse() if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err) + return core.InternalServerError(), core.NewLoggedError("Error while generating proxy response: %v", err) } return proxyResponse, nil diff --git a/core/types.go b/core/types.go index d3f5a9e..b04c547 100644 --- a/core/types.go +++ b/core/types.go @@ -7,11 +7,16 @@ import ( "github.com/aws/aws-lambda-go/events" ) -// GatewayTimeout returns a dafault Gateway Timeout (504) response +// GatewayTimeout returns Gateway Timeout (504) response func GatewayTimeout() events.APIGatewayProxyResponse { return events.APIGatewayProxyResponse{StatusCode: http.StatusGatewayTimeout} } +// InternalServerError returns Internal Server Error (500) response +func InternalServerError() events.APIGatewayProxyResponse { + return events.APIGatewayProxyResponse{StatusCode: http.StatusInternalServerError} +} + // NewLoggedError generates a new error and logs it to stdout func NewLoggedError(format string, a ...interface{}) error { err := fmt.Errorf(format, a...) diff --git a/gin/adapter.go b/gin/adapter.go index 9372449..2888747 100644 --- a/gin/adapter.go +++ b/gin/adapter.go @@ -34,7 +34,7 @@ func (g *GinLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayP ginRequest, err := g.ProxyEventToHTTPRequest(req) if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err) + return core.InternalServerError(), core.NewLoggedError("Could not convert proxy event to request: %v", err) } respWriter := core.NewProxyResponseWriter() @@ -42,7 +42,7 @@ func (g *GinLambda) Proxy(req events.APIGatewayProxyRequest) (events.APIGatewayP proxyResponse, err := respWriter.GetProxyResponse() if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err) + return core.InternalServerError(), core.NewLoggedError("Error while generating proxy response: %v", err) } return proxyResponse, nil diff --git a/gorillamux/adapter.go b/gorillamux/adapter.go index d5623c6..7e19351 100644 --- a/gorillamux/adapter.go +++ b/gorillamux/adapter.go @@ -22,7 +22,7 @@ func New(router *mux.Router) *GorillaMuxAdapter { func (h *GorillaMuxAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { req, err := h.ProxyEventToHTTPRequest(event) if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err) + return core.InternalServerError(), core.NewLoggedError("Could not convert proxy event to request: %v", err) } w := core.NewProxyResponseWriter() @@ -30,7 +30,7 @@ func (h *GorillaMuxAdapter) Proxy(event events.APIGatewayProxyRequest) (events.A resp, err := w.GetProxyResponse() if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err) + return core.InternalServerError(), core.NewLoggedError("Error while generating proxy response: %v", err) } return resp, nil diff --git a/handlerfunc/adapter.go b/handlerfunc/adapter.go index f39f598..8f3419f 100644 --- a/handlerfunc/adapter.go +++ b/handlerfunc/adapter.go @@ -21,7 +21,7 @@ func New(handlerFunc http.HandlerFunc) *HandlerFuncAdapter { func (h *HandlerFuncAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { req, err := h.ProxyEventToHTTPRequest(event) if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err) + return core.InternalServerError(), core.NewLoggedError("Could not convert proxy event to request: %v", err) } w := core.NewProxyResponseWriter() @@ -29,7 +29,7 @@ func (h *HandlerFuncAdapter) Proxy(event events.APIGatewayProxyRequest) (events. resp, err := w.GetProxyResponse() if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err) + return core.InternalServerError(), core.NewLoggedError("Error while generating proxy response: %v", err) } return resp, nil diff --git a/httpadapter/adapter.go b/httpadapter/adapter.go index 0f3032c..62cd0ee 100644 --- a/httpadapter/adapter.go +++ b/httpadapter/adapter.go @@ -21,7 +21,7 @@ func New(handler http.Handler) *HandlerAdapter { func (h *HandlerAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { req, err := h.ProxyEventToHTTPRequest(event) if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err) + return core.InternalServerError(), core.NewLoggedError("Could not convert proxy event to request: %v", err) } w := core.NewProxyResponseWriter() @@ -29,7 +29,7 @@ func (h *HandlerAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIG resp, err := w.GetProxyResponse() if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err) + return core.InternalServerError(), core.NewLoggedError("Error while generating proxy response: %v", err) } return resp, nil diff --git a/negroni/adapter.go b/negroni/adapter.go index 74e1ce9..8e0659f 100644 --- a/negroni/adapter.go +++ b/negroni/adapter.go @@ -22,7 +22,7 @@ func New(n *negroni.Negroni) *NegroniAdapter { func (h *NegroniAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { req, err := h.ProxyEventToHTTPRequest(event) if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Could not convert proxy event to request: %v", err) + return core.InternalServerError(), core.NewLoggedError("Could not convert proxy event to request: %v", err) } w := core.NewProxyResponseWriter() @@ -30,7 +30,7 @@ func (h *NegroniAdapter) Proxy(event events.APIGatewayProxyRequest) (events.APIG resp, err := w.GetProxyResponse() if err != nil { - return core.GatewayTimeout(), core.NewLoggedError("Error while generating proxy response: %v", err) + return core.InternalServerError(), core.NewLoggedError("Error while generating proxy response: %v", err) } return resp, nil