@@ -17,6 +17,8 @@ import Prelude.Compat
18import Control.DeepSeq
18import Control.DeepSeq
19 (NFData (..))
19 (NFData (..))
20+import Control.Exception
20import Control.Monad.Catch
22import Control.Monad.Catch
21 (Exception)
23 (Exception)
22import Data.Bifoldable
24import Data.Bifoldable
@@ -38,7 +40,7 @@ import Data.Text
38import Data.Text.Encoding
40import Data.Text.Encoding
39 (encodeUtf8)
41 (encodeUtf8)
40import Data.Typeable
42import Data.Typeable
41− (Typeable)
43+ (Typeable, typeOf)
42import GHC.Generics
44import GHC.Generics
44import Network.HTTP.Media
46import Network.HTTP.Media
@@ -66,19 +68,36 @@ data ServantError =
66 -- | The content-type header is invalid
68 -- | The content-type header is invalid
67 | InvalidContentTypeHeader Response
69 | InvalidContentTypeHeader Response
68 -- | There was a connection error, and no response was received
70 -- | There was a connection error, and no response was received
69− | ConnectionError Text
71+ | ConnectionError SomeException
70− deriving (Eq, Show, Generic, Typeable)
72+ deriving (Show, Generic, Typeable)
74+instance Eq ServantError where
75+ FailureResponse req res == FailureResponse req' res' = req == req' && res == res'
76+ DecodeFailure t r == DecodeFailure t' r' = t == t' && r == r'
77+ UnsupportedContentType mt r == UnsupportedContentType mt' r' = mt == mt' && r == r'
78+ InvalidContentTypeHeader r == InvalidContentTypeHeader r' = r == r'
79+ ConnectionError exc == ConnectionError exc' = eqSomeException exc exc'
81+ -- returns true, if type of exception is the same
82+ eqSomeException (SomeException a) (SomeException b) = typeOf a == typeOf b
84+ -- prevent wild card blindness
85+ FailureResponse {} == _ = False
86+ DecodeFailure {} == _ = False
87+ UnsupportedContentType {} == _ = False
88+ InvalidContentTypeHeader {} == _ = False
89+ ConnectionError {} == _ = False
72instance Exception ServantError
91instance Exception ServantError
93+-- | Note: an exception in 'ConnectionError' might not be evaluated fully,
94+-- We only 'rnf' its 'show'ed value.
74instance NFData ServantError where
95instance NFData ServantError where
75 rnf (FailureResponse req res) = rnf req `seq` rnf res
96 rnf (FailureResponse req res) = rnf req `seq` rnf res
76 rnf (DecodeFailure err res) = rnf err `seq` rnf res
97 rnf (DecodeFailure err res) = rnf err `seq` rnf res
77− rnf (UnsupportedContentType mt' res) =
98+ rnf (UnsupportedContentType mt' res) = mediaTypeRnf mt' `seq` rnf res
78− mediaTypeRnf mt' `seq`
80 rnf (InvalidContentTypeHeader res) = rnf res
99 rnf (InvalidContentTypeHeader res) = rnf res
81− rnf (ConnectionError err) = rnf err
100+ rnf (ConnectionError err) = err `seq` rnf (show err)
83mediaTypeRnf :: MediaType -> ()
102mediaTypeRnf :: MediaType -> ()
84mediaTypeRnf mt =
103mediaTypeRnf mt =