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) ++
        ": " ++ displayException e ++ " on " ++ showReq req
      resp $
        W.responseLBS
          st
          [(hContentType, "text/plain")]
          (fromStrict $ statusMessage st)
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) ++
        ": " ++ displayException e ++ " on " ++ showReq req
      resp $
        W.responseLBS
          st
          [(hContentType, "text/plain")]
          (fromStrict $ statusMessage st)
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) ++
        ": " ++ displayException e ++ " on " ++ showReq req
      resp $
        W.responseLBS
          st
          [(hContentType, "text/plain")]
          (fromStrict $ statusMessage st)