Created
January 29, 2017 07:09
-
-
Save naoto-ogawa/3383e1d2c5405a1eb7ade304b3de3469 to your computer and use it in GitHub Desktop.
Exception handing by catch in Servant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Exception as E | |
import Servant | |
import Control.Monad.IO.Class -- liftIO | |
import Network.Wai.Handler.Warp -- run | |
type FileRead02 = "sample02.txt" :> Get '[PlainText] String | |
myAPI02 :: Proxy FileRead02 | |
myAPI02 = Proxy | |
app02 :: Application | |
app02 = serve myAPI02 server02 | |
server02 :: Server FileRead02 | |
server02 = do | |
f <- liftIO (readF "sample02.txt") | |
return f | |
readF :: String -> IO String | |
readF x = (readFile x) `E.catch` (\(SomeException x) -> return "error!!") | |
-- catch :: Exception e => IO a -> (e -> IO a) -> IO a | |
main02 :: IO () | |
main02 = run 8080 app02 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment