{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings     #-}

module HaskellWorks.CabalCache.AppError
  ( AwsError(..),
    HttpError(..),
    HasStatusCode(..),
    HasMaybeStatusCode(..),
    displayAwsError,
    displayHttpError,
  ) where

import HaskellWorks.Prelude

import qualified Network.HTTP.Client as HTTP
import qualified Network.HTTP.Types as HTTP

newtype AwsError = AwsError
  { AwsError -> Status
status :: HTTP.Status
  }
  deriving (AwsError -> AwsError -> Bool
(AwsError -> AwsError -> Bool)
-> (AwsError -> AwsError -> Bool) -> Eq AwsError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsError -> AwsError -> Bool
== :: AwsError -> AwsError -> Bool
$c/= :: AwsError -> AwsError -> Bool
/= :: AwsError -> AwsError -> Bool
Eq, Int -> AwsError -> ShowS
[AwsError] -> ShowS
AwsError -> String
(Int -> AwsError -> ShowS)
-> (AwsError -> String) -> ([AwsError] -> ShowS) -> Show AwsError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsError -> ShowS
showsPrec :: Int -> AwsError -> ShowS
$cshow :: AwsError -> String
show :: AwsError -> String
$cshowList :: [AwsError] -> ShowS
showList :: [AwsError] -> ShowS
Show, (forall x. AwsError -> Rep AwsError x)
-> (forall x. Rep AwsError x -> AwsError) -> Generic AwsError
forall x. Rep AwsError x -> AwsError
forall x. AwsError -> Rep AwsError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AwsError -> Rep AwsError x
from :: forall x. AwsError -> Rep AwsError x
$cto :: forall x. Rep AwsError x -> AwsError
to :: forall x. Rep AwsError x -> AwsError
Generic)

data HttpError = HttpError
  { HttpError -> Request
reasponse :: HTTP.Request
  , HttpError -> HttpExceptionContent
content    :: HTTP.HttpExceptionContent
  }
  deriving (Int -> HttpError -> ShowS
[HttpError] -> ShowS
HttpError -> String
(Int -> HttpError -> ShowS)
-> (HttpError -> String)
-> ([HttpError] -> ShowS)
-> Show HttpError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HttpError -> ShowS
showsPrec :: Int -> HttpError -> ShowS
$cshow :: HttpError -> String
show :: HttpError -> String
$cshowList :: [HttpError] -> ShowS
showList :: [HttpError] -> ShowS
Show, (forall x. HttpError -> Rep HttpError x)
-> (forall x. Rep HttpError x -> HttpError) -> Generic HttpError
forall x. Rep HttpError x -> HttpError
forall x. HttpError -> Rep HttpError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HttpError -> Rep HttpError x
from :: forall x. HttpError -> Rep HttpError x
$cto :: forall x. Rep HttpError x -> HttpError
to :: forall x. Rep HttpError x -> HttpError
Generic)

displayAwsError :: AwsError -> Text
displayAwsError :: AwsError -> Text
displayAwsError (AwsError Status
s) = Status -> Text
forall a. Show a => a -> Text
tshow Status
s

displayHttpError :: HttpError -> Text
displayHttpError :: HttpError -> Text
displayHttpError (HttpError Request
_ HttpExceptionContent
s) = HttpExceptionContent -> Text
forall a. Show a => a -> Text
tshow HttpExceptionContent
s

class HasStatusCode a where
  statusCodeOf :: a -> Int

class HasMaybeStatusCode a where
  maybeStatusCodeOf :: a -> Maybe Int

instance HasStatusCode AwsError where
  statusCodeOf :: AwsError -> Int
statusCodeOf (AwsError (HTTP.Status Int
c ByteString
_)) = Int
c

instance HasMaybeStatusCode AwsError where
  maybeStatusCodeOf :: AwsError -> Maybe Int
maybeStatusCodeOf (AwsError (HTTP.Status Int
c ByteString
_)) = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
c

instance HasMaybeStatusCode HttpError where
  maybeStatusCodeOf :: HttpError -> Maybe Int
maybeStatusCodeOf (HttpError Request
_ HttpExceptionContent
content') = case HttpExceptionContent
content' of
    HTTP.StatusCodeException Response ()
response ByteString
_ -> let HTTP.Status Int
c ByteString
_ = Response () -> Status
forall body. Response body -> Status
HTTP.responseStatus Response ()
response in Int -> Maybe Int
forall a. a -> Maybe a
Just Int
c
    HttpExceptionContent
_ -> Maybe Int
forall a. Maybe a
Nothing