{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE DeriveFoldable        #-}
{-# LANGUAGE DeriveFunctor         #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DeriveTraversable     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
module Servant.Client.Core.ClientError (
    ClientError (..),
    ) where

import           Prelude ()
import           Prelude.Compat

import           Control.DeepSeq
                 (NFData (..))
import           Control.Exception
                 (SomeException (..))
import           Control.Monad.Catch
                 (Exception)
import qualified Data.ByteString              as BS
import           Data.Text
                 (Text)
import           Data.Typeable
                 (Typeable, typeOf)
import           GHC.Generics
                 (Generic)
import           Network.HTTP.Media
                 (MediaType)
import           Network.HTTP.Types ()

import           Servant.Client.Core.BaseUrl
import           Servant.Client.Core.Internal
                 (mediaTypeRnf)
import           Servant.Client.Core.Request
import           Servant.Client.Core.Response


-- | A type representing possible errors in a request
--
-- Note that this type substantially changed in 0.12.
data ClientError =
  -- | The server returned an error response including the
  -- failing request. 'requestPath' includes the 'BaseUrl' and the
  -- path of the request.
    FailureResponse (RequestF () (BaseUrl, BS.ByteString)) Response
  -- | The body could not be decoded at the expected type
  | DecodeFailure Text Response
  -- | The content-type of the response is not supported
  | UnsupportedContentType MediaType Response
  -- | The content-type header is invalid
  | InvalidContentTypeHeader Response
  -- | There was a connection error, and no response was received
  | ConnectionError SomeException
  deriving (Int -> ClientError -> ShowS
[ClientError] -> ShowS
ClientError -> String
(Int -> ClientError -> ShowS)
-> (ClientError -> String)
-> ([ClientError] -> ShowS)
-> Show ClientError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ClientError -> ShowS
showsPrec :: Int -> ClientError -> ShowS
$cshow :: ClientError -> String
show :: ClientError -> String
$cshowList :: [ClientError] -> ShowS
showList :: [ClientError] -> ShowS
Show, (forall x. ClientError -> Rep ClientError x)
-> (forall x. Rep ClientError x -> ClientError)
-> Generic ClientError
forall x. Rep ClientError x -> ClientError
forall x. ClientError -> Rep ClientError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ClientError -> Rep ClientError x
from :: forall x. ClientError -> Rep ClientError x
$cto :: forall x. Rep ClientError x -> ClientError
to :: forall x. Rep ClientError x -> ClientError
Generic, Typeable)

instance Eq ClientError where
  FailureResponse RequestF () (BaseUrl, ByteString)
req ResponseF ByteString
res     == :: ClientError -> ClientError -> Bool
== FailureResponse RequestF () (BaseUrl, ByteString)
req' ResponseF ByteString
res'     = RequestF () (BaseUrl, ByteString)
req RequestF () (BaseUrl, ByteString)
-> RequestF () (BaseUrl, ByteString) -> Bool
forall a. Eq a => a -> a -> Bool
== RequestF () (BaseUrl, ByteString)
req' Bool -> Bool -> Bool
&& ResponseF ByteString
res ResponseF ByteString -> ResponseF ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ResponseF ByteString
res'
  DecodeFailure Text
t ResponseF ByteString
r           == DecodeFailure Text
t' ResponseF ByteString
r'           = Text
t Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
t' Bool -> Bool -> Bool
&& ResponseF ByteString
r ResponseF ByteString -> ResponseF ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ResponseF ByteString
r'
  UnsupportedContentType MediaType
mt ResponseF ByteString
r == UnsupportedContentType MediaType
mt' ResponseF ByteString
r' = MediaType
mt MediaType -> MediaType -> Bool
forall a. Eq a => a -> a -> Bool
== MediaType
mt' Bool -> Bool -> Bool
&& ResponseF ByteString
r ResponseF ByteString -> ResponseF ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ResponseF ByteString
r'
  InvalidContentTypeHeader ResponseF ByteString
r  == InvalidContentTypeHeader ResponseF ByteString
r'   = ResponseF ByteString
r ResponseF ByteString -> ResponseF ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ResponseF ByteString
r'
  ConnectionError SomeException
exc         == ConnectionError SomeException
exc'          = SomeException -> SomeException -> Bool
eqSomeException SomeException
exc SomeException
exc'
    where
      -- returns true, if type of exception is the same
      eqSomeException :: SomeException -> SomeException -> Bool
eqSomeException (SomeException e
a) (SomeException e
b) = e -> SomeTypeRep
forall a. Typeable a => a -> SomeTypeRep
typeOf e
a SomeTypeRep -> SomeTypeRep -> Bool
forall a. Eq a => a -> a -> Bool
== e -> SomeTypeRep
forall a. Typeable a => a -> SomeTypeRep
typeOf e
b

  -- prevent wild card blindness
  FailureResponse          {} == ClientError
_ = Bool
False
  DecodeFailure            {} == ClientError
_ = Bool
False
  UnsupportedContentType   {} == ClientError
_ = Bool
False
  InvalidContentTypeHeader {} == ClientError
_ = Bool
False
  ConnectionError          {} == ClientError
_ = Bool
False

instance Exception ClientError

-- | Note: an exception in 'ConnectionError' might not be evaluated fully,
-- We only 'rnf' its 'show'ed value.
instance NFData ClientError where
    rnf :: ClientError -> ()
rnf (FailureResponse RequestF () (BaseUrl, ByteString)
req ResponseF ByteString
res)        = RequestF () (BaseUrl, ByteString) -> ()
forall a. NFData a => a -> ()
rnf RequestF () (BaseUrl, ByteString)
req () -> () -> ()
forall a b. a -> b -> b
`seq` ResponseF ByteString -> ()
forall a. NFData a => a -> ()
rnf ResponseF ByteString
res
    rnf (DecodeFailure Text
err ResponseF ByteString
res)          = Text -> ()
forall a. NFData a => a -> ()
rnf Text
err () -> () -> ()
forall a b. a -> b -> b
`seq` ResponseF ByteString -> ()
forall a. NFData a => a -> ()
rnf ResponseF ByteString
res
    rnf (UnsupportedContentType MediaType
mt' ResponseF ByteString
res) = MediaType -> ()
mediaTypeRnf MediaType
mt' () -> () -> ()
forall a b. a -> b -> b
`seq` ResponseF ByteString -> ()
forall a. NFData a => a -> ()
rnf ResponseF ByteString
res
    rnf (InvalidContentTypeHeader ResponseF ByteString
res)   = ResponseF ByteString -> ()
forall a. NFData a => a -> ()
rnf ResponseF ByteString
res
    rnf (ConnectionError SomeException
err)            = SomeException
err SomeException -> () -> ()
forall a b. a -> b -> b
`seq` String -> ()
forall a. NFData a => a -> ()
rnf (SomeException -> String
forall a. Show a => a -> String
show SomeException
err)