Skip to content

Instantly share code, notes, and snippets.

@naoto-ogawa
Created January 29, 2017 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naoto-ogawa/3383e1d2c5405a1eb7ade304b3de3469 to your computer and use it in GitHub Desktop.
Save naoto-ogawa/3383e1d2c5405a1eb7ade304b3de3469 to your computer and use it in GitHub Desktop.
Exception handing by catch in Servant
{-# 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