Skip to content

Commit

Permalink
Merge pull request #207 from well-typed/edsko/error-response-body
Browse files Browse the repository at this point in the history
Ensure we include response body for server errors
  • Loading branch information
edsko authored Jul 26, 2024
2 parents e991d40 + 3e634f4 commit a2133e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Network/GRPC/Spec/Serialization/Headers/Response.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ classifyServerResponse rpc status headers mBody
-- almost certainly not be a content-type header present in the
-- case of a non-200 HTTP status. We don't want to synthesize /that/
-- error, so we override it.
Right Nothing
case trailersOnlyContentType parsed of
Left _err -> Right Nothing
Right mCType -> Right mCType
, trailersOnlyProper = parsedTrailers {
properTrailersGrpcStatus = Right $
GrpcError err
Expand All @@ -132,7 +134,6 @@ classifyServerResponse rpc status headers mBody
_otherwise -> Just defaultMsg
}
}

where
parsed :: TrailersOnly' GrpcException
parsed = parseTrailersOnly' rpc headers
Expand Down
28 changes: 27 additions & 1 deletion test-grapesy/Test/Sanity/BrokenDeployments.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import Proto.API.Ping

tests :: TestTree
tests = testGroup "Test.Sanity.BrokenDeployments" [
testCase "statusNon200" test_statusNon200
testGroup "status" [
testCase "non200" test_statusNon200
, testCase "non200Body" test_statusNon200Body
]
, testGroup "ContentType" [
testCase "nonGrpcRegular" test_nonGrpcContentTypeRegular
, testCase "missingRegular" test_missingContentTypeRegular
Expand Down Expand Up @@ -66,6 +69,29 @@ test_statusNon200 = respondWith response $ \addr -> do
responseStatus = HTTP.badRequest400
}

-- | Ensure that we include the response body for errors, if any
test_statusNon200Body :: Assertion
test_statusNon200Body = respondWith response $ \addr -> do
mResp :: Either GrpcException (Proto PongMessage) <- try $
Client.withConnection connParams (Client.ServerInsecure addr) $ \conn ->
Client.withRPC conn def (Proxy @Ping) $ \call -> do
Client.sendFinalInput call defMessage
fst <$> Client.recvFinalOutput call
case mResp of
Left err
| grpcError err == GrpcInternal
, Just msg <- grpcErrorMessage err
, "Server supplied custom error" `Text.isInfixOf` msg ->
return ()
_otherwise ->
assertFailure $ "Unexpected response: " ++ show mResp
where
response :: Response
response = def {
responseStatus = HTTP.badRequest400
, responseBody = "Server supplied custom error"
}

{-------------------------------------------------------------------------------
Content-type
-------------------------------------------------------------------------------}
Expand Down

0 comments on commit a2133e9

Please sign in to comment.