Haskell 如何从处理程序中抛出异常
我正在构建一个RESTful端点,如果存在无效数据,它将抛出一个错误。下面是一个简化(有点傻)的版本:Haskell 如何从处理程序中抛出异常,haskell,yesod,Haskell,Yesod,我正在构建一个RESTful端点,如果存在无效数据,它将抛出一个错误。下面是一个简化(有点傻)的版本: 因此,如果它在选择opt时出错,我希望它短路,而不继续处理处理程序的其余部分。您可以使用sendResponseStatus直接指定响应: getEventsR = do mpage <- lookupGetParam "page" case hasPage mpage of Left message -> sendResponseStatus status40
因此,如果它在
选择opt时出错,我希望它短路,而不继续处理处理程序的其余部分。您可以使用sendResponseStatus
直接指定响应:
getEventsR = do
mpage <- lookupGetParam "page"
case hasPage mpage of
Left message -> sendResponseStatus status400 message
Right val -> ...continue processing with val...
HCError
是HandlerContents
类型的构造函数:
ErrorResponse
具有以下构造函数:
data ErrorResponse:
NotFound
InternalError Text
InvalidArgs [Text]
NotAuthenticated
PermissionDenied Text
BadMethod Method
因此,您可以通过发送数量有限的错误,并且错误消息仅限于文本您希望“抛出异常”做什么?使服务器崩溃?返回HTTP响应(如500或404?)随消息返回HTTP响应(如400)。(例如,{error:“这应该会出错”}
谢谢。我更新了问题,以更好地反映我为什么使用术语“抛出异常”,例如,我相信事件
getEventsR = do
mpage <- lookupGetParam "page"
case hasPage mpage of
Left message -> sendResponseStatus status400 message
Right val -> ...continue processing with val...
get404 key = do
mres <- get key
case mres of
Nothing -> notFound'
Just res -> return res
notFound' :: MonadIO m => m a
notFound' = liftIO $ throwIO $ HCError NotFound
data HandlerContents:
HCContent Status !TypedContent
HCError ErrorResponse
HCSendFile ContentType FilePath (Maybe FilePart)
HCRedirect Status Text
HCCreated Text
HCWai Response
HCWaiApp Application
data ErrorResponse:
NotFound
InternalError Text
InvalidArgs [Text]
NotAuthenticated
PermissionDenied Text
BadMethod Method