Catch multiple Exception with Lenses

Viewed 88

I am using Lens with Amazonka to deal with errors but I struggle to deal with errors:

doSignup e p = (AWS.send $ AWS.signUp "secret" e p)
    $> Right ()
    & catching_ AWS._UsernameExistsException (return $ Left AlreadyExistingEmail)
    & catching_ AWS._InvalidPasswordException (return $ Left WeakPassword)
    & catching  AWS._SertviceError (return . UnknownSignUpError)

data SignUpError where
  AlreadyExistingEmail ::                          SignUpError
  NotAnEmail           ::                          SignUpError
  WeakPassword         ::                          SignUpError
  UnknownSignUpError   :: forall a. Show a => a -> SignUpError

I struggle to have a consistent catching behavior, when a _UsernameExistsException is thrown I got a Left WeakPassword.

It becomes weirder because it works when I drop the WeakPassword line.

While I get the right error (keeping only the last line):

       expected: Right ()
        but got: Left UnknownSignUpError ServiceError' {_serviceAbbrev = Abbrev "CognitoIdentityProvider", _serviceStatus = Status {statusCode = 400, statusMessage = "Bad Request"}, _serviceHeaders = [("Date","Tue, 06 Oct 2020 05:38:56 GMT"),("Content-Type","application/x-amz-json-1.1"),("Content-Length","96"),("Connection","keep-alive"),("x-amzn-RequestId","b09210a3-41ed-46ee-af4f-46db58b98695"),("x-amzn-ErrorType","UsernameExistsException:"),("x-amzn-ErrorMessage","An account with the given email already exists.")], _serviceCode = ErrorCode "UsernameExists", _serviceMessage = Just (ErrorMessage "An account with the given email already exists."), _serviceRequestId = Just (RequestId "b09210a3-41ed-46ee-af4f-46db58b98695")}

I have tried to use catches but handler requires Typeable Lenses which is not the case.

How can I have a "pattern matching-like" way to deal with exceptions? Thanks in advance.

1 Answers

In fact Control.Lens.catching was used instead of Network.AWS.Prelude.catching which was messing up the exception handling.

Related