From 41133e1f2e83fff0dea8a5a1ce1ea53b2d9aa2d8 Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Sat, 13 May 2017 23:45:56 +0300 Subject: Respond with 502 on every backend exception From RFC 7231: 6.6.3. 502 Bad Gateway The 502 (Bad Gateway) status code indicates that the server, while acting as a gateway or proxy, received an invalid response from an inbound server it accessed while attempting to fulfill the request. --- src/Sproxy/Application.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Sproxy/Application.hs b/src/Sproxy/Application.hs index 5de9474..3d6598f 100644 --- a/src/Sproxy/Application.hs +++ b/src/Sproxy/Application.hs @@ -30,8 +30,8 @@ import Network.HTTP.Conduit (requestBodySourceChunkedIO, requestBodySourceIO) import Network.HTTP.Types (RequestHeaders, ResponseHeaders, methodGet, methodPost) import Network.HTTP.Types.Header ( hConnection, hContentLength, hContentType, hCookie, hLocation, hTransferEncoding ) -import Network.HTTP.Types.Status ( Status(..), badRequest400, forbidden403, found302, - internalServerError500, methodNotAllowed405, movedPermanently301, +import Network.HTTP.Types.Status ( Status(..), badGateway502, badRequest400, forbidden403, + found302, internalServerError500, methodNotAllowed405, movedPermanently301, networkAuthenticationRequired511, notFound404, ok200, seeOther303, temporaryRedirect307 ) import Network.Socket (NameInfoFlag(NI_NUMERICHOST), getNameInfo) import Network.Wai.Conduit (sourceRequestBody, responseSource) @@ -383,12 +383,16 @@ notFound msg req resp = do logException :: W.Middleware logException app req resp = catches (app req resp) [ + Handler badGateway, Handler internalError ] where internalError :: SomeException -> IO W.ResponseReceived internalError = response internalServerError500 + badGateway :: BE.HttpException -> IO W.ResponseReceived + badGateway = response badGateway502 + response :: Exception e => Status -> e -> IO W.ResponseReceived response st e = do Log.error $ show (statusCode st) ++ " " ++ unpack (statusMessage st) -- cgit v1.2.3