我想在servant处理程序monad中处理数据库异常。
我尝试使用 try
包中的 Control.Exception
函数来与 Left exception -> throwError err422 { errBody = ... }
进行大小写匹配。
我正在使用 postgresql-typed 与 PostgreSQL 数据库连接。我想捕获 PGError 异常。
relevant code 有以下修改:
accountHandler :: CreateAccountPayload -> Handler Account
accountHandler payload =
let errors = validateCreateAccountPayload payload in
if hasErrors errors then
throwError err422 { errBody = JSON.encode errors }
else
do
result <- try (liftIO (insertAccount payload))
case result of
Right account -> return account
Left exception -> throwError err422 { errBody = JSON.encode [ValidationError (Text.pack "email") (Text.pack "is already taken")] }
我希望能够从数据库调用中捕获结果并能够对其进行大小写匹配。这些案例应该是一个异常(exception)或一个值。我目前收到以下编译错误:
src/Main.hs:64:17: error:
• Couldn't match type ‘IO’ with ‘Handler’
Expected type: Handler (Either e0 Account)
Actual type: IO (Either e0 Account)
• In a stmt of a 'do' block:
result <- try (liftIO (insertAccount payload))
In the expression:
do result <- try (liftIO (insertAccount payload))
case result of
Right account -> return account
Left exception -> throwError err422 {errBody = encode ...}
In the expression:
if hasErrors errors then
throwError err422 {errBody = encode errors}
else
do result <- try (liftIO (insertAccount payload))
case result of
Right account -> return account
Left exception -> throwError ...
|
64 | result <- try (liftIO (insertAccount payload))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
最佳答案
我认为 liftIO
将您的 IO Account
结果提升到 Handler
monad 中。如果是这样,也许 try
应该先行,liftIO
次之:
result <- liftIO (try (insertAccount payload))
case result of
Right account -> return account
Left (PGError e) -> throwError err422