Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions services/brig/src/Brig/Provider/API.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions services/brig/src/Brig/Provider/RPC.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)@.
Expand All @@ -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)
Comment thread
elland marked this conversation as resolved.
Outdated

extReq :: ServiceConn -> [ByteString] -> Request -> Request
extReq scon ps =
Expand Down