Skip to content

Commit

Permalink
[feat] rethrow errors as wai errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MangoIV committed May 13, 2024
1 parent 58bc1eb commit aa96349
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 12 additions & 0 deletions libs/wire-subsystems/src/Wire/UserSubsystem.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ import Imports
import Polysemy
import Wire.API.Federation.Error
import Wire.API.User
import qualified Network.Wai.Utilities as Wai
import Wire.API.Error
-- FUTUREWORK(mangoiv): this should probably be renamed such that it doesn't
-- associate with the name "brig" anymore
import Wire.API.Error.Brig (BrigError(..))

-- | All errors that are thrown by the user subsystem are subsumed under this sum type.
data UserSubsystemError
= UserSubsystemDisplayNameManagedByScim
-- ^ user is managed by scim or e2ei is enabled
-- FUTUREWORK(mangoiv): the name should probably resemble that
| UserSubsystemProfileNotFound
deriving (Eq, Show)

userSubsystemErrorToWai :: UserSubsystemError -> Wai.Error
userSubsystemErrorToWai = dynErrorToWai . \case
UserSubsystemProfileNotFound -> dynError @(MapError UserNotFound)
UserSubsystemDisplayNameManagedByScim -> dynError @(MapError NameManagedByScim)

instance Exception UserSubsystemError

data AllowSCIMUpdates
Expand Down
15 changes: 9 additions & 6 deletions services/brig/src/Brig/CanonicalInterpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ import Wire.UserStore
import Wire.UserStore.Cassandra
import Wire.UserSubsystem
import Wire.UserSubsystem.Interpreter
import qualified Network.Wai.Utilities as Wai

type BrigCanonicalEffects =
'[ UserSubsystem,
DeleteQueue,
UserEvents,
Error UserSubsystemError,
Error Wire.API.Federation.Error.FederationError,
Error Wai.Error,
Wire.FederationAPIAccess.FederationAPIAccess Wire.API.Federation.Client.FederatorClient,
UserStore,
SFT,
Expand Down Expand Up @@ -149,18 +151,19 @@ runBrigToIO e (AppT ma) = do
. interpretSFT (e ^. httpManager)
. interpretUserStoreCassandra (e ^. casClient)
. interpretFederationAPIAccess federationApiAccessConfig
. throwFederationErrorAsWaiError
. mapError @UserSubsystemError SomeException
. rethrowWaiErrorIO
. mapError federationErrorToWai
. mapError userSubsystemErrorToWai
. interpretUserEvents
. runDeleteQueue (e ^. internalEvents)
. runUserSubsystem userSubsystemConfig
)
)
$ runReaderT ma e

throwFederationErrorAsWaiError :: Member (Final IO) r => InterpreterFor (Error FederationError) r
throwFederationErrorAsWaiError action = do
eithError <- errorToIOFinal action
rethrowWaiErrorIO :: Member (Final IO) r => InterpreterFor (Error Wai.Error) r
rethrowWaiErrorIO act = do
eithError <- errorToIOFinal act
case eithError of
Left err -> embedToFinal $ throwM $ federationErrorToWai err
Left err -> embedToFinal $ throwM $ err
Right a -> pure a

0 comments on commit aa96349

Please sign in to comment.