使用 Lenses 捕获多个异常

Catch multiple Exception with Lenses

我正在使用 LensAmazonka 来处理错误,但我很难处理错误:

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

我努力保持一致的接球行为,当抛出 _UsernameExistsException 时,我得到了 Left WeakPassword

它变得更奇怪了,因为它在我放下 WeakPassword 行时起作用。

虽然我得到正确的错误(只保留最后一行):

       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")}

我曾尝试使用 catches,但 handler 需要 Typeable 镜头,但事实并非如此。

我怎样才能有一种“类似模式匹配”的方式来处理异常?提前致谢。

事实上,Control.Lens.catching 被用来代替 Network.AWS.Prelude.catching,这搞乱了异常处理。