on master help sign in
Merge pull request #89 from PierreR/master

Use MonadThrow instead of Either in the signature of parseBaseUrl

commite8f7c69aca650a19d141ac24edb36de7146d53cc authorJulian Arni <jkarni@gmail.com> dateThu 4 Jun 2015, 14:32 (11 years ago) parentd654cca3bcbe80
2 files changed+28−10
servant-client/src/Servant/Common/BaseUrl.hs+23−9 servant-client/src/Servant/Common/Req.hs+5−1
servant-client/src/Servant/Common/BaseUrl.hs+23−9
@@ -1,8 +1,19 @@
1−{-# LANGUAGE DeriveGeneric #-}
2−{-# LANGUAGE ViewPatterns #-}
3−module Servant.Common.BaseUrl where
1+{-# LANGUAGE DeriveDataTypeable #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE ViewPatterns #-}
4+module Servant.Common.BaseUrl (
5+ -- * types
6+ BaseUrl (..)
7+ , InvalidBaseUrlException
8+ , Scheme (..)
9+ -- * functions
10+ , parseBaseUrl
11+ , showBaseUrl
12+) where
413
14+import Control.Monad.Catch (MonadThrow, throwM, Exception)
515import Data.List
16+import Data.Typeable
617import GHC.Generics
718import Network.URI
819import Safe
@@ -34,20 +45,23 @@ showBaseUrl (BaseUrl urlscheme host port) =
3445 (Https, 443) -> ""
3546 _ -> ":" ++ show port
3647
37−parseBaseUrl :: String -> Either String BaseUrl
48+data InvalidBaseUrlException = InvalidBaseUrlException String deriving (Show, Typeable)
49+instance Exception InvalidBaseUrlException
50+
51+parseBaseUrl :: MonadThrow m => String -> m BaseUrl
3852parseBaseUrl s = case parseURI (removeTrailingSlash s) of
3953 -- This is a rather hacky implementation and should be replaced with something
4054 -- implemented in attoparsec (which is already a dependency anyhow (via aeson)).
4155 Just (URI "http:" (Just (URIAuth "" host (':' : (readMaybe -> Just port)))) "" "" "") ->
42− Right (BaseUrl Http host port)
56+ return (BaseUrl Http host port)
4357 Just (URI "http:" (Just (URIAuth "" host "")) "" "" "") ->
44− Right (BaseUrl Http host 80)
58+ return (BaseUrl Http host 80)
4559 Just (URI "https:" (Just (URIAuth "" host (':' : (readMaybe -> Just port)))) "" "" "") ->
46− Right (BaseUrl Https host port)
60+ return (BaseUrl Https host port)
4761 Just (URI "https:" (Just (URIAuth "" host "")) "" "" "") ->
48− Right (BaseUrl Https host 443)
62+ return (BaseUrl Https host 443)
4963 _ -> if "://" `isInfixOf` s
50− then Left ("invalid base url: " ++ s)
64+ then throwM (InvalidBaseUrlException $ "Invalid base URL: " ++ s)
5165 else parseBaseUrl ("http://" ++ s)
5266 where
5367 removeTrailingSlash str = case lastMay str of
servant-client/src/Servant/Common/Req.hs+5−1
@@ -1,3 +1,4 @@
1+{-# LANGUAGE DeriveDataTypeable #-}
12{-# LANGUAGE CPP #-}
23{-# LANGUAGE OverloadedStrings #-}
34{-# LANGUAGE ScopedTypeVariables #-}
@@ -18,6 +19,7 @@ import Data.String.Conversions
1819import Data.Proxy
1920import Data.Text (Text)
2021import Data.Text.Encoding
22+import Data.Typeable
2123import Network.HTTP.Client hiding (Proxy)
2224import Network.HTTP.Client.TLS
2325import Network.HTTP.Media
@@ -53,7 +55,9 @@ data ServantError
5355 { responseContentTypeHeader :: ByteString
5456 , responseBody :: ByteString
5557 }
56− deriving (Show)
58+ deriving (Show, Typeable)
59+
60+instance Exception ServantError
5761
5862data Req = Req
5963 { reqPath :: String