From 88a13d524f7c1042277ba683c54c6d0e09eb7bf4 Mon Sep 17 00:00:00 2001 From: Igor Ranieri Date: Mon, 12 Feb 2024 10:47:08 +0100 Subject: [PATCH 1/2] Added reason to bad gateway error for RPCs. --- services/brig/src/Brig/Provider/API.hs | 9 +++++---- services/brig/src/Brig/Provider/RPC.hs | 6 +++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/services/brig/src/Brig/Provider/API.hs b/services/brig/src/Brig/Provider/API.hs index d6cb9698153..0bc3963beeb 100644 --- a/services/brig/src/Brig/Provider/API.hs +++ b/services/brig/src/Brig/Provider/API.hs @@ -76,6 +76,7 @@ import Data.Range import Data.Set qualified as Set import Data.Text.Ascii qualified as Ascii import Data.Text.Encoding qualified as Text +import Data.Text.Lazy qualified as Text import GHC.TypeNats import Imports import Network.HTTP.Types.Status @@ -705,7 +706,7 @@ addBot zuid zcon cid add = do -- implicitly in the next line. pure $ FutureWork @'UnprotectedBot undefined wrapClientE (User.addClient (botUserId bid) bcl newClt maxPermClients (Just $ Set.singleton Public.ClientSupportsLegalholdImplicitConsent)) - !>> const (StdError badGateway) -- MalformedPrekeys + !>> const (StdError $ badGatewayWith "MalformedPrekeys") -- Add the bot to the conversation ev <- lift $ RPC.addBotMember zuid zcon cid bid (clientId clt) pid sid @@ -917,8 +918,8 @@ maybeInvalidUser = maybe (throwStd (errorToWai @'E.InvalidUser)) pure rangeChecked :: (KnownNat n, KnownNat m, Within a n m, Monad monad) => a -> (ExceptT Error monad) (Range n m a) rangeChecked = either (throwStd . invalidRange . fromString) pure . checkedEither -badGateway :: Wai.Error -badGateway = Wai.mkError status502 "bad-gateway" "The upstream service returned an invalid response." +badGatewayWith :: String -> Wai.Error +badGatewayWith str = Wai.mkError status502 "bad-gateway" ("The upstream service returned an invalid response: " <> Text.pack str) tooManyBots :: Wai.Error tooManyBots = Wai.mkError status409 "too-many-bots" "Maximum number of bots for the service reached." @@ -927,7 +928,7 @@ serviceNotWhitelisted :: Wai.Error serviceNotWhitelisted = Wai.mkError status403 "service-not-whitelisted" "The desired service is not on the whitelist of allowed services for this team." serviceError :: RPC.ServiceError -> Wai.Error -serviceError RPC.ServiceUnavailable = badGateway +serviceError (RPC.ServiceUnavailableWith str) = badGatewayWith str serviceError RPC.ServiceBotConflict = tooManyBots randServiceToken :: MonadIO m => m Public.ServiceToken diff --git a/services/brig/src/Brig/Provider/RPC.hs b/services/brig/src/Brig/Provider/RPC.hs index bdb00994d4d..66f9b248035 100644 --- a/services/brig/src/Brig/Provider/RPC.hs +++ b/services/brig/src/Brig/Provider/RPC.hs @@ -63,7 +63,7 @@ import Wire.Rpc -- External RPC data ServiceError - = ServiceUnavailable + = ServiceUnavailableWith String | ServiceBotConflict -- | Request a new bot to be created by an external service. @@ -86,7 +86,7 @@ createBot scon new = do case Bilge.statusCode rs of 201 -> decodeBytes "External" (responseBody rs) 409 -> throwE ServiceBotConflict - _ -> lift (extLogError scon rs) >> throwE ServiceUnavailable + _ -> lift (extLogError scon rs) >> throwE (ServiceUnavailableWith $ show rs) where -- we can't use 'responseJsonEither' instead, because we have a @Response ByteString@ -- here, not a @Response (Maybe ByteString)@. @@ -97,7 +97,7 @@ createBot scon new = do extReq scon ["bots"] . method POST . Bilge.json new - onExc ex = lift (extLogError scon ex) >> throwE ServiceUnavailable + onExc ex = lift (extLogError scon ex) >> throwE (ServiceUnavailableWith $ show ex) extReq :: ServiceConn -> [ByteString] -> Request -> Request extReq scon ps = From 931b53d287d18a6fb03fe140d906e314606b0204 Mon Sep 17 00:00:00 2001 From: Igor Ranieri Date: Mon, 12 Feb 2024 15:01:46 +0100 Subject: [PATCH 2/2] Use displayException. --- services/brig/src/Brig/Provider/RPC.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/brig/src/Brig/Provider/RPC.hs b/services/brig/src/Brig/Provider/RPC.hs index 66f9b248035..d11dc336a8c 100644 --- a/services/brig/src/Brig/Provider/RPC.hs +++ b/services/brig/src/Brig/Provider/RPC.hs @@ -97,7 +97,7 @@ createBot scon new = do extReq scon ["bots"] . method POST . Bilge.json new - onExc ex = lift (extLogError scon ex) >> throwE (ServiceUnavailableWith $ show ex) + onExc ex = lift (extLogError scon ex) >> throwE (ServiceUnavailableWith $ displayException ex) extReq :: ServiceConn -> [ByteString] -> Request -> Request extReq scon ps =