-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | The @amazonka@ edge of the transport-fault vocabulary. An adapter folds the AWS error sum into
"Ecluse.Core.Fault" as a value, or raises it past the adapter as 'AwsRequestFailed'. A credential
failure that reaches no adapter, such as a background refresh, renders through 'displayAwsFaultT'.
No form holds the request or the response headers, and a service's message keeps at most its first
line.
-}
module Ecluse.Runtime.Aws.Fault (
    -- * Faults as values
    classifyAwsTransport,
    classifyEscapedTransport,
    sendClassified,
    foldTransportEscapes,
    serviceStatus,

    -- * Faults raised past the adapter
    AwsRequestFailed (..),
    AwsParty (..),
    AwsFault (..),
    AwsAnswer (..),
    sendRaising,
    retypeAwsFailure,

    -- * Failures that reach no adapter
    AwsCredentialFault (..),
    retypeAmazonkaEscapes,
    displayAwsFaultT,

    -- * Error codes
    errorCodeText,
) where

import Amazonka qualified as AWS
import Amazonka.Auth.Exception (AuthError (..))
import Control.Monad.Trans.Resource (runResourceT)
import Data.Text qualified as T
import Lens.Micro ((^.))
import Network.HTTP.Client qualified as HTTP
import Network.HTTP.Types.Status (statusCode)
import UnliftIO (MonadUnliftIO)
import UnliftIO.Exception (throwIO, tryJust)

import Ecluse.Core.Fault (HttpFault (HttpStatus), TransportCause (TransportProtocol), TransportFault, boundedDetail, renderHttpFault, transportFault)
import Ecluse.Core.Fault.Http (classifyTransport, displayFaultT, hostPortLabel, httpFault)

-- | Classify an @amazonka@ error into the core transport vocabulary. A service's answer is detailed as 'AwsAnswer' renders it.
classifyAwsTransport :: AWS.Error -> TransportFault
classifyAwsTransport :: Error -> TransportFault
classifyAwsTransport = \case
    AWS.TransportError HttpException
httpErr -> HttpException -> TransportFault
classifyTransport HttpException
httpErr
    AWS.ServiceError ServiceError
service -> TransportCause -> Text -> TransportFault
transportFault TransportCause
TransportProtocol (Text -> AwsAnswer -> Text
renderAnswer (ServiceError -> Text
serviceName ServiceError
service) (ServiceError -> AwsAnswer
serviceAnswer ServiceError
service))
    AWS.SerializeError SerializeError
failure -> TransportCause -> Text -> TransportFault
transportFault TransportCause
TransportProtocol (Text -> Int -> Text
renderUndecodable (SerializeError
failure SerializeError -> Getting Text SerializeError Text -> Text
forall s a. s -> Getting a s a -> a
^. (Abbrev -> Const Text Abbrev)
-> SerializeError -> Const Text SerializeError
Lens' SerializeError Abbrev
AWS.serializeError_abbrev ((Abbrev -> Const Text Abbrev)
 -> SerializeError -> Const Text SerializeError)
-> ((Text -> Const Text Text) -> Abbrev -> Const Text Abbrev)
-> Getting Text SerializeError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Const Text Text) -> Abbrev -> Const Text Abbrev
Iso' Abbrev Text
AWS._Abbrev) (SerializeError -> Int
undecodableStatus SerializeError
failure))

-- | Send a request with the AWS error folded into the caller's own fault type, a value rather than an exception.
sendClassified ::
    (AWS.AWSRequest a) =>
    (AWS.Error -> e) ->
    AWS.Env ->
    a ->
    IO (Either e (AWS.AWSResponse a))
sendClassified :: forall a e.
AWSRequest a =>
(Error -> e) -> Env -> a -> IO (Either e (AWSResponse a))
sendClassified Error -> e
classify Env
env = (Either Error (AWSResponse a) -> Either e (AWSResponse a))
-> IO (Either Error (AWSResponse a))
-> IO (Either e (AWSResponse a))
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Error -> e)
-> Either Error (AWSResponse a) -> Either e (AWSResponse a)
forall a b c. (a -> b) -> Either a c -> Either b c
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Error -> e
classify) (IO (Either Error (AWSResponse a))
 -> IO (Either e (AWSResponse a)))
-> (a -> IO (Either Error (AWSResponse a)))
-> a
-> IO (Either e (AWSResponse a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ResourceT IO (Either Error (AWSResponse a))
-> IO (Either Error (AWSResponse a))
forall (m :: * -> *) a. MonadUnliftIO m => ResourceT m a -> m a
runResourceT (ResourceT IO (Either Error (AWSResponse a))
 -> IO (Either Error (AWSResponse a)))
-> (a -> ResourceT IO (Either Error (AWSResponse a)))
-> a
-> IO (Either Error (AWSResponse a))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Env -> a -> ResourceT IO (Either Error (AWSResponse a))
forall (m :: * -> *) a.
(MonadResource m, AWSRequest a) =>
Env -> a -> m (Either Error (AWSResponse a))
AWS.sendEither Env
env

-- | Classify only escaping AWS errors and bare HTTP exceptions, excluding credential and typed wrapper exceptions.
classifyEscapedTransport :: SomeException -> Maybe TransportFault
classifyEscapedTransport :: SomeException -> Maybe TransportFault
classifyEscapedTransport SomeException
err
    | Just HttpException
http <- SomeException -> Maybe HttpException
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
err = TransportFault -> Maybe TransportFault
forall a. a -> Maybe a
Just (HttpException -> TransportFault
classifyTransport HttpException
http)
    | Just Error
aws <- SomeException -> Maybe Error
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
err = TransportFault -> Maybe TransportFault
forall a. a -> Maybe a
Just (Error -> TransportFault
classifyAwsTransport Error
aws)
    | Bool
otherwise = Maybe TransportFault
forall a. Maybe a
Nothing

-- | Fold AWS and bare body-read transport exceptions into values. Other exceptions propagate.
foldTransportEscapes :: (TransportFault -> fault) -> IO (Either fault a) -> IO (Either fault a)
foldTransportEscapes :: forall fault a.
(TransportFault -> fault)
-> IO (Either fault a) -> IO (Either fault a)
foldTransportEscapes TransportFault -> fault
inject IO (Either fault a)
action =
    (SomeException -> Maybe TransportFault)
-> IO (Either fault a)
-> IO (Either TransportFault (Either fault a))
forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust SomeException -> Maybe TransportFault
classifyEscapedTransport IO (Either fault a)
action IO (Either TransportFault (Either fault a))
-> (Either TransportFault (Either fault a) -> IO (Either fault a))
-> IO (Either fault a)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (TransportFault -> IO (Either fault a))
-> (Either fault a -> IO (Either fault a))
-> Either TransportFault (Either fault a)
-> IO (Either fault a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either fault a -> IO (Either fault a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either fault a -> IO (Either fault a))
-> (TransportFault -> Either fault a)
-> TransportFault
-> IO (Either fault a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. fault -> Either fault a
forall a b. a -> Either a b
Left (fault -> Either fault a)
-> (TransportFault -> fault) -> TransportFault -> Either fault a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TransportFault -> fault
inject) Either fault a -> IO (Either fault a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | The HTTP status a service answered with, or 'Nothing' when the request never got an answer.
serviceStatus :: AWS.Error -> Maybe Int
serviceStatus :: Error -> Maybe Int
serviceStatus = \case
    AWS.ServiceError ServiceError
err -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Status -> Int
statusCode (ServiceError
err ServiceError -> Getting Status ServiceError Status -> Status
forall s a. s -> Getting a s a -> a
^. Getting Status ServiceError Status
Lens' ServiceError Status
AWS.serviceError_status))
    Error
_ -> Maybe Int
forall a. Maybe a
Nothing

-- | A failed @amazonka@ request raised past its adapter. It renders without the request: http-client redacts only @Authorization@, so a session-token header would print.
data AwsRequestFailed = AwsRequestFailed
    { AwsRequestFailed -> AwsParty
arfParty :: AwsParty
    , AwsRequestFailed -> AwsFault
arfFault :: AwsFault
    }
    deriving stock (AwsRequestFailed -> AwsRequestFailed -> Bool
(AwsRequestFailed -> AwsRequestFailed -> Bool)
-> (AwsRequestFailed -> AwsRequestFailed -> Bool)
-> Eq AwsRequestFailed
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsRequestFailed -> AwsRequestFailed -> Bool
== :: AwsRequestFailed -> AwsRequestFailed -> Bool
$c/= :: AwsRequestFailed -> AwsRequestFailed -> Bool
/= :: AwsRequestFailed -> AwsRequestFailed -> Bool
Eq, Int -> AwsRequestFailed -> ShowS
[AwsRequestFailed] -> ShowS
AwsRequestFailed -> FilePath
(Int -> AwsRequestFailed -> ShowS)
-> (AwsRequestFailed -> FilePath)
-> ([AwsRequestFailed] -> ShowS)
-> Show AwsRequestFailed
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsRequestFailed -> ShowS
showsPrec :: Int -> AwsRequestFailed -> ShowS
$cshow :: AwsRequestFailed -> FilePath
show :: AwsRequestFailed -> FilePath
$cshowList :: [AwsRequestFailed] -> ShowS
showList :: [AwsRequestFailed] -> ShowS
Show)

instance Exception AwsRequestFailed where
    displayException :: AwsRequestFailed -> FilePath
displayException = Text -> FilePath
forall a. ToString a => a -> FilePath
toString (Text -> FilePath)
-> (AwsRequestFailed -> Text) -> AwsRequestFailed -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AwsRequestFailed -> Text
renderRequestFailed

-- | Where a failed request went.
data AwsParty
    = -- | The host and port it was sent to.
      AwsEndpointAt Text Int
    | -- | The service that answered, named when no request is in reach.
      AwsServiceNamed Text
    deriving stock (AwsParty -> AwsParty -> Bool
(AwsParty -> AwsParty -> Bool)
-> (AwsParty -> AwsParty -> Bool) -> Eq AwsParty
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsParty -> AwsParty -> Bool
== :: AwsParty -> AwsParty -> Bool
$c/= :: AwsParty -> AwsParty -> Bool
/= :: AwsParty -> AwsParty -> Bool
Eq, Int -> AwsParty -> ShowS
[AwsParty] -> ShowS
AwsParty -> FilePath
(Int -> AwsParty -> ShowS)
-> (AwsParty -> FilePath) -> ([AwsParty] -> ShowS) -> Show AwsParty
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsParty -> ShowS
showsPrec :: Int -> AwsParty -> ShowS
$cshow :: AwsParty -> FilePath
show :: AwsParty -> FilePath
$cshowList :: [AwsParty] -> ShowS
showList :: [AwsParty] -> ShowS
Show)

-- | Why a request failed.
data AwsFault
    = -- | The service refused it.
      AwsStatus AwsAnswer
    | -- | The service answered with this HTTP status, in a body @amazonka@ could not decode.
      AwsUndecodable Int
    | -- | The HTTP exchange failed: a status from an endpoint outside the AWS protocol, or a transport fault.
      AwsHttp HttpFault
    deriving stock (AwsFault -> AwsFault -> Bool
(AwsFault -> AwsFault -> Bool)
-> (AwsFault -> AwsFault -> Bool) -> Eq AwsFault
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsFault -> AwsFault -> Bool
== :: AwsFault -> AwsFault -> Bool
$c/= :: AwsFault -> AwsFault -> Bool
/= :: AwsFault -> AwsFault -> Bool
Eq, Int -> AwsFault -> ShowS
[AwsFault] -> ShowS
AwsFault -> FilePath
(Int -> AwsFault -> ShowS)
-> (AwsFault -> FilePath) -> ([AwsFault] -> ShowS) -> Show AwsFault
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsFault -> ShowS
showsPrec :: Int -> AwsFault -> ShowS
$cshow :: AwsFault -> FilePath
show :: AwsFault -> FilePath
$cshowList :: [AwsFault] -> ShowS
showList :: [AwsFault] -> ShowS
Show)

-- | A service's refusal without its response headers. A signature error's message can quote the signed request, so it is dropped.
data AwsAnswer = AwsAnswer
    { AwsAnswer -> Int
aaStatus :: Int
    , AwsAnswer -> Text
aaCode :: Text
    , AwsAnswer -> Maybe Text
aaRequestId :: Maybe Text
    , AwsAnswer -> Maybe Text
aaMessage :: Maybe Text
    -- ^ The first line of any other message. For an access denial, it names the IAM action and resource.
    }
    deriving stock (AwsAnswer -> AwsAnswer -> Bool
(AwsAnswer -> AwsAnswer -> Bool)
-> (AwsAnswer -> AwsAnswer -> Bool) -> Eq AwsAnswer
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsAnswer -> AwsAnswer -> Bool
== :: AwsAnswer -> AwsAnswer -> Bool
$c/= :: AwsAnswer -> AwsAnswer -> Bool
/= :: AwsAnswer -> AwsAnswer -> Bool
Eq, Int -> AwsAnswer -> ShowS
[AwsAnswer] -> ShowS
AwsAnswer -> FilePath
(Int -> AwsAnswer -> ShowS)
-> (AwsAnswer -> FilePath)
-> ([AwsAnswer] -> ShowS)
-> Show AwsAnswer
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsAnswer -> ShowS
showsPrec :: Int -> AwsAnswer -> ShowS
$cshow :: AwsAnswer -> FilePath
show :: AwsAnswer -> FilePath
$cshowList :: [AwsAnswer] -> ShowS
showList :: [AwsAnswer] -> ShowS
Show)

-- | Send a request, raising its failure as 'AwsRequestFailed'. A service's answer is named by the endpoint the env sent the request to.
sendRaising :: (AWS.AWSRequest a) => AWS.Env -> a -> IO (AWS.AWSResponse a)
sendRaising :: forall a. AWSRequest a => Env -> a -> IO (AWSResponse a)
sendRaising Env
env a
request = (Error -> SomeException)
-> Env -> a -> IO (Either SomeException (AWSResponse a))
forall a e.
AWSRequest a =>
(Error -> e) -> Env -> a -> IO (Either e (AWSResponse a))
sendClassified ((Text -> AwsParty) -> Error -> SomeException
raised (AwsParty -> Text -> AwsParty
forall a b. a -> b -> a
const (Env -> a -> AwsParty
forall a. AWSRequest a => Env -> a -> AwsParty
requestEndpoint Env
env a
request))) Env
env a
request IO (Either SomeException (AWSResponse a))
-> (Either SomeException (AWSResponse a) -> IO (AWSResponse a))
-> IO (AWSResponse a)
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SomeException -> IO (AWSResponse a))
-> (AWSResponse a -> IO (AWSResponse a))
-> Either SomeException (AWSResponse a)
-> IO (AWSResponse a)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SomeException -> IO (AWSResponse a)
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO AWSResponse a -> IO (AWSResponse a)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Raise the @amazonka@ or http-client failure of an action whose request the caller never holds, such as credential discovery, as a typed fault. An unparseable URL leaves as http-client's own exception.
retypeAwsFailure :: IO a -> IO a
retypeAwsFailure :: forall a. IO a -> IO a
retypeAwsFailure IO a
action =
    -- Thrown outside any handler, so no annotation of the throw carries the original along.
    (SomeException -> Maybe SomeException)
-> IO a -> IO (Either SomeException a)
forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust SomeException -> Maybe SomeException
awsCallFault IO a
action IO (Either SomeException a)
-> (Either SomeException a -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SomeException -> IO a)
-> (a -> IO a) -> Either SomeException a -> IO a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SomeException -> IO a
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO a -> IO a
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Why @amazonka@ found no usable credentials when no request failed. It keeps the first line of amazonka's reason, never a file's contents.
data AwsCredentialFault
    = -- | A setting a provider reads is missing: an environment variable or a file.
      AwsCredentialMissing Text
    | -- | A credentials file or document could not be used.
      AwsCredentialUnusable Text
    | -- | No provider in the chain found credentials.
      AwsCredentialsExhausted
    deriving stock (AwsCredentialFault -> AwsCredentialFault -> Bool
(AwsCredentialFault -> AwsCredentialFault -> Bool)
-> (AwsCredentialFault -> AwsCredentialFault -> Bool)
-> Eq AwsCredentialFault
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsCredentialFault -> AwsCredentialFault -> Bool
== :: AwsCredentialFault -> AwsCredentialFault -> Bool
$c/= :: AwsCredentialFault -> AwsCredentialFault -> Bool
/= :: AwsCredentialFault -> AwsCredentialFault -> Bool
Eq, Int -> AwsCredentialFault -> ShowS
[AwsCredentialFault] -> ShowS
AwsCredentialFault -> FilePath
(Int -> AwsCredentialFault -> ShowS)
-> (AwsCredentialFault -> FilePath)
-> ([AwsCredentialFault] -> ShowS)
-> Show AwsCredentialFault
forall a.
(Int -> a -> ShowS) -> (a -> FilePath) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsCredentialFault -> ShowS
showsPrec :: Int -> AwsCredentialFault -> ShowS
$cshow :: AwsCredentialFault -> FilePath
show :: AwsCredentialFault -> FilePath
$cshowList :: [AwsCredentialFault] -> ShowS
showList :: [AwsCredentialFault] -> ShowS
Show)

instance Exception AwsCredentialFault where
    displayException :: AwsCredentialFault -> FilePath
displayException = Text -> FilePath
forall a. ToString a => a -> FilePath
toString (Text -> FilePath)
-> (AwsCredentialFault -> Text) -> AwsCredentialFault -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AwsCredentialFault -> Text
renderCredentialFault

-- | Rethrow an @amazonka@ error or credential failure escaping the action as its typed fault. Any other exception passes unchanged.
retypeAmazonkaEscapes :: (MonadUnliftIO m) => m a -> m a
retypeAmazonkaEscapes :: forall (m :: * -> *) a. MonadUnliftIO m => m a -> m a
retypeAmazonkaEscapes m a
action = (SomeException -> Maybe SomeException)
-> m a -> m (Either SomeException a)
forall (m :: * -> *) e b a.
(MonadUnliftIO m, Exception e) =>
(e -> Maybe b) -> m a -> m (Either b a)
tryJust SomeException -> Maybe SomeException
amazonkaFault m a
action m (Either SomeException a)
-> (Either SomeException a -> m a) -> m a
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (SomeException -> m a)
-> (a -> m a) -> Either SomeException a -> m a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either SomeException -> m a
forall (m :: * -> *) e a. (MonadIO m, Exception e) => e -> m a
throwIO a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | 'displayFaultT', with an @amazonka@ error or credential failure rendered as its typed fault's own text.
displayAwsFaultT :: SomeException -> Text
displayAwsFaultT :: SomeException -> Text
displayAwsFaultT SomeException
err = SomeException -> Text
displayFaultT (SomeException -> Maybe SomeException -> SomeException
forall a. a -> Maybe a -> a
fromMaybe SomeException
err (SomeException -> Maybe SomeException
amazonkaFault SomeException
err))

-- amazonka's background refresh hands a failure to the thread that discovered the credentials, wrapped in 'AuthError'.
amazonkaFault :: SomeException -> Maybe SomeException
amazonkaFault :: SomeException -> Maybe SomeException
amazonkaFault SomeException
err
    | Just Error
awsErr <- SomeException -> Maybe Error
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
err = SomeException -> Maybe SomeException
forall a. a -> Maybe a
Just ((Text -> AwsParty) -> Error -> SomeException
raised Text -> AwsParty
AwsServiceNamed Error
awsErr)
    | Just AuthError
authErr <- SomeException -> Maybe AuthError
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
err = SomeException -> Maybe SomeException
forall a. a -> Maybe a
Just (AuthError -> SomeException
authFault AuthError
authErr)
    | Bool
otherwise = Maybe SomeException
forall a. Maybe a
Nothing

authFault :: AuthError -> SomeException
authFault :: AuthError -> SomeException
authFault = \case
    RetrievalError HttpException
httpErr -> HttpException -> SomeException
raisedHttp HttpException
httpErr
    AuthServiceError ServiceError
service -> (Text -> AwsParty) -> Error -> SomeException
raised Text -> AwsParty
AwsServiceNamed (ServiceError -> Error
AWS.ServiceError ServiceError
service)
    OtherAuthError SomeException
inner -> SomeException -> Maybe SomeException -> SomeException
forall a. a -> Maybe a -> a
fromMaybe SomeException
inner (SomeException -> Maybe SomeException
awsCallFault SomeException
inner)
    MissingEnvError Text
reason -> AwsCredentialFault -> SomeException
forall e. Exception e => e -> SomeException
toException (Text -> AwsCredentialFault
AwsCredentialMissing (Text -> Text
firstLineOf Text
reason))
    MissingFileError FilePath
path -> AwsCredentialFault -> SomeException
forall e. Exception e => e -> SomeException
toException (Text -> AwsCredentialFault
AwsCredentialMissing (FilePath -> Text
forall a. ToText a => a -> Text
toText FilePath
path))
    InvalidFileError Text
reason -> AwsCredentialFault -> SomeException
forall e. Exception e => e -> SomeException
toException (Text -> AwsCredentialFault
AwsCredentialUnusable (Text -> Text
firstLineOf Text
reason))
    InvalidIAMError Text
reason -> AwsCredentialFault -> SomeException
forall e. Exception e => e -> SomeException
toException (Text -> AwsCredentialFault
AwsCredentialUnusable (Text -> Text
firstLineOf Text
reason))
    AuthError
CredentialChainExhausted -> AwsCredentialFault -> SomeException
forall e. Exception e => e -> SomeException
toException AwsCredentialFault
AwsCredentialsExhausted
  where
    firstLineOf :: Text -> Text
firstLineOf = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" (Maybe Text -> Text) -> (Text -> Maybe Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe Text
firstLine

-- An http-client failure counts too, as the caller knows the call was one of amazonka's, such as a container credential fetch.
awsCallFault :: SomeException -> Maybe SomeException
awsCallFault :: SomeException -> Maybe SomeException
awsCallFault SomeException
err = SomeException -> Maybe SomeException
amazonkaFault SomeException
err Maybe SomeException -> Maybe SomeException -> Maybe SomeException
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (HttpException -> SomeException
raisedHttp (HttpException -> SomeException)
-> Maybe HttpException -> Maybe SomeException
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SomeException -> Maybe HttpException
forall e. Exception e => SomeException -> Maybe e
fromException SomeException
err)

-- | An error code lowercased, so no comparison turns on a service's own casing.
errorCodeText :: AWS.ErrorCode -> Text
errorCodeText :: ErrorCode -> Text
errorCodeText ErrorCode
code = Text -> Text
T.toLower (ErrorCode
code ErrorCode -> Getting Text ErrorCode Text -> Text
forall s a. s -> Getting a s a -> a
^. Getting Text ErrorCode Text
Iso' ErrorCode Text
AWS._ErrorCode)

-- A service's answer is named by @answering@, given the service's name.
raised :: (Text -> AwsParty) -> AWS.Error -> SomeException
raised :: (Text -> AwsParty) -> Error -> SomeException
raised Text -> AwsParty
answering = \case
    AWS.TransportError HttpException
httpErr -> HttpException -> SomeException
raisedHttp HttpException
httpErr
    AWS.ServiceError ServiceError
service -> AwsRequestFailed -> SomeException
forall e. Exception e => e -> SomeException
toException (AwsParty -> AwsFault -> AwsRequestFailed
AwsRequestFailed (Text -> AwsParty
answering (ServiceError -> Text
serviceName ServiceError
service)) (AwsAnswer -> AwsFault
AwsStatus (ServiceError -> AwsAnswer
serviceAnswer ServiceError
service)))
    AWS.SerializeError SerializeError
failure -> AwsRequestFailed -> SomeException
forall e. Exception e => e -> SomeException
toException (AwsParty -> AwsFault -> AwsRequestFailed
AwsRequestFailed (Text -> AwsParty
answering (SerializeError
failure SerializeError -> Getting Text SerializeError Text -> Text
forall s a. s -> Getting a s a -> a
^. (Abbrev -> Const Text Abbrev)
-> SerializeError -> Const Text SerializeError
Lens' SerializeError Abbrev
AWS.serializeError_abbrev ((Abbrev -> Const Text Abbrev)
 -> SerializeError -> Const Text SerializeError)
-> ((Text -> Const Text Text) -> Abbrev -> Const Text Abbrev)
-> Getting Text SerializeError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Const Text Text) -> Abbrev -> Const Text Abbrev
Iso' Abbrev Text
AWS._Abbrev)) (Int -> AwsFault
AwsUndecodable (SerializeError -> Int
undecodableStatus SerializeError
failure)))

-- An unparseable URL leaves as the client's own exception, which "Ecluse.Core.Fault.Http" renders by its authority.
raisedHttp :: HTTP.HttpException -> SomeException
raisedHttp :: HttpException -> SomeException
raisedHttp = \case
    err :: HttpException
err@(HTTP.HttpExceptionRequest Request
request HttpExceptionContent
_) -> AwsRequestFailed -> SomeException
forall e. Exception e => e -> SomeException
toException (AwsParty -> AwsFault -> AwsRequestFailed
AwsRequestFailed (Text -> Int -> AwsParty
AwsEndpointAt (ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 (Request -> ByteString
HTTP.host Request
request)) (Request -> Int
HTTP.port Request
request)) (HttpFault -> AwsFault
AwsHttp (HttpException -> HttpFault
httpFault HttpException
err)))
    HttpException
err -> HttpException -> SomeException
forall e. Exception e => e -> SomeException
toException HttpException
err

serviceAnswer :: AWS.ServiceError -> AwsAnswer
serviceAnswer :: ServiceError -> AwsAnswer
serviceAnswer ServiceError
service =
    AwsAnswer
        { aaStatus :: Int
aaStatus = Status -> Int
statusCode (ServiceError
service ServiceError -> Getting Status ServiceError Status -> Status
forall s a. s -> Getting a s a -> a
^. Getting Status ServiceError Status
Lens' ServiceError Status
AWS.serviceError_status)
        , aaCode :: Text
aaCode = ErrorCode
code ErrorCode -> Getting Text ErrorCode Text -> Text
forall s a. s -> Getting a s a -> a
^. Getting Text ErrorCode Text
Iso' ErrorCode Text
AWS._ErrorCode
        , aaRequestId :: Maybe Text
aaRequestId = RequestId -> Text
AWS.fromRequestId (RequestId -> Text) -> Maybe RequestId -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ServiceError
service ServiceError
-> Getting (Maybe RequestId) ServiceError (Maybe RequestId)
-> Maybe RequestId
forall s a. s -> Getting a s a -> a
^. Getting (Maybe RequestId) ServiceError (Maybe RequestId)
Lens' ServiceError (Maybe RequestId)
AWS.serviceError_requestId
        , aaMessage :: Maybe Text
aaMessage = if ErrorCode -> Text
errorCodeText ErrorCode
code Text -> [Text] -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`elem` [Text]
signatureCodes then Maybe Text
forall a. Maybe a
Nothing else Text -> Maybe Text
firstLine (Text -> Maybe Text)
-> (ErrorMessage -> Text) -> ErrorMessage -> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorMessage -> Text
AWS.fromErrorMessage (ErrorMessage -> Maybe Text) -> Maybe ErrorMessage -> Maybe Text
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ServiceError
service ServiceError
-> Getting (Maybe ErrorMessage) ServiceError (Maybe ErrorMessage)
-> Maybe ErrorMessage
forall s a. s -> Getting a s a -> a
^. Getting (Maybe ErrorMessage) ServiceError (Maybe ErrorMessage)
Lens' ServiceError (Maybe ErrorMessage)
AWS.serviceError_message
        }
  where
    code :: ErrorCode
code = ServiceError
service ServiceError
-> Getting ErrorCode ServiceError ErrorCode -> ErrorCode
forall s a. s -> Getting a s a -> a
^. Getting ErrorCode ServiceError ErrorCode
Lens' ServiceError ErrorCode
AWS.serviceError_code

-- The codes whose message AWS documents as quoting the canonical request, spelled as AWS sends them.
signatureCodes :: [Text]
signatureCodes :: [Text]
signatureCodes = (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (ErrorCode -> Text
errorCodeText (ErrorCode -> Text) -> (Text -> ErrorCode) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ErrorCode
AWS.newErrorCode) [Text
"InvalidSignatureException", Text
"SignatureDoesNotMatch", Text
"IncompleteSignature"]

firstLine :: Text -> Maybe Text
firstLine :: Text -> Maybe Text
firstLine Text
message = case (Char -> Bool) -> Text -> Text
T.takeWhile (Char -> FilePath -> Bool
forall (f :: * -> *) a.
(Foldable f, DisallowElem f, Eq a) =>
a -> f a -> Bool
`notElem` [Char
'\n', Char
'\r']) Text
message of
    Text
"" -> Maybe Text
forall a. Maybe a
Nothing
    Text
line -> Text -> Maybe Text
forall a. a -> Maybe a
Just Text
line

serviceName :: AWS.ServiceError -> Text
serviceName :: ServiceError -> Text
serviceName ServiceError
service = ServiceError
service ServiceError -> Getting Text ServiceError Text -> Text
forall s a. s -> Getting a s a -> a
^. (Abbrev -> Const Text Abbrev)
-> ServiceError -> Const Text ServiceError
Lens' ServiceError Abbrev
AWS.serviceError_abbrev ((Abbrev -> Const Text Abbrev)
 -> ServiceError -> Const Text ServiceError)
-> ((Text -> Const Text Text) -> Abbrev -> Const Text Abbrev)
-> Getting Text ServiceError Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Const Text Text) -> Abbrev -> Const Text Abbrev
Iso' Abbrev Text
AWS._Abbrev

renderCredentialFault :: AwsCredentialFault -> Text
renderCredentialFault :: AwsCredentialFault -> Text
renderCredentialFault = \case
    AwsCredentialMissing Text
reason -> Text
"AWS credentials missing: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
reason
    AwsCredentialUnusable Text
reason -> Text
"AWS credentials unusable: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
reason
    AwsCredentialFault
AwsCredentialsExhausted -> Text
"AWS credential discovery found no credentials"

renderRequestFailed :: AwsRequestFailed -> Text
renderRequestFailed :: AwsRequestFailed -> Text
renderRequestFailed AwsRequestFailed
failed = Text
"AWS request failed at " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AwsRequestFailed -> Text
renderFailure AwsRequestFailed
failed

renderFailure :: AwsRequestFailed -> Text
renderFailure :: AwsRequestFailed -> Text
renderFailure (AwsRequestFailed AwsParty
party AwsFault
fault) = case AwsFault
fault of
    AwsStatus AwsAnswer
answer -> Text -> AwsAnswer -> Text
renderAnswer (AwsParty -> Text
renderParty AwsParty
party) AwsAnswer
answer
    AwsUndecodable Int
status -> Text -> Int -> Text
renderUndecodable (AwsParty -> Text
renderParty AwsParty
party) Int
status
    AwsHttp HttpFault
http -> Text -> HttpFault -> Text
renderHttpFault (AwsParty -> Text
renderParty AwsParty
party) HttpFault
http

renderParty :: AwsParty -> Text
renderParty :: AwsParty -> Text
renderParty = \case
    AwsEndpointAt Text
endpointHost Int
endpointPort -> Text -> Int -> Text
hostPortLabel Text
endpointHost Int
endpointPort
    AwsServiceNamed Text
service -> Text
service

-- Bounded here, so the classified detail and the raised fault keep one log-line budget.
renderAnswer :: Text -> AwsAnswer -> Text
renderAnswer :: Text -> AwsAnswer -> Text
renderAnswer Text
source AwsAnswer
answer =
    Text -> Text
boundedDetail (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$
        Text -> HttpFault -> Text
renderHttpFault Text
source (Int -> HttpFault
HttpStatus (AwsAnswer -> Int
aaStatus AwsAnswer
answer))
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> AwsAnswer -> Text
aaCode AwsAnswer
answer)
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (\Text
requestId -> Text
" (request " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
requestId Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")") (AwsAnswer -> Maybe Text
aaRequestId AwsAnswer
answer)
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Text
": " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (AwsAnswer -> Maybe Text
aaMessage AwsAnswer
answer)

renderUndecodable :: Text -> Int -> Text
renderUndecodable :: Text -> Int -> Text
renderUndecodable Text
source Int
status = Text -> HttpFault -> Text
renderHttpFault Text
source (Int -> HttpFault
HttpStatus Int
status) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", an answer that could not be decoded"

-- The body holds the whole answer, credentials included, and the decoder's message can quote it, so only the status is kept.
undecodableStatus :: AWS.SerializeError -> Int
undecodableStatus :: SerializeError -> Int
undecodableStatus SerializeError
failure = Status -> Int
statusCode (SerializeError
failure SerializeError -> Getting Status SerializeError Status -> Status
forall s a. s -> Getting a s a -> a
^. Getting Status SerializeError Status
Lens' SerializeError Status
AWS.serializeError_status)

-- The @host:port@ an env sends a request to, by the service resolution @amazonka@ applies.
requestEndpoint :: (AWS.AWSRequest a) => AWS.Env -> a -> AwsParty
requestEndpoint :: forall a. AWSRequest a => Env -> a -> AwsParty
requestEndpoint Env
env a
request = Text -> Int -> AwsParty
AwsEndpointAt (ByteString -> Text
forall a b. ConvertUtf8 a b => b -> a
decodeUtf8 (Endpoint
endpoint Endpoint -> Getting ByteString Endpoint ByteString -> ByteString
forall s a. s -> Getting a s a -> a
^. Getting ByteString Endpoint ByteString
Lens' Endpoint ByteString
AWS.endpoint_host)) (Endpoint
endpoint Endpoint -> Getting Int Endpoint Int -> Int
forall s a. s -> Getting a s a -> a
^. Getting Int Endpoint Int
Lens' Endpoint Int
AWS.endpoint_port)
  where
    service :: Service
service = (Service -> Service) -> a -> Request a
forall a. AWSRequest a => (Service -> Service) -> a -> Request a
AWS.request (Env -> Service -> Service
forall (withAuth :: * -> *). Env' withAuth -> Service -> Service
AWS.overrides Env
env) a
request Request a -> Getting Service (Request a) Service -> Service
forall s a. s -> Getting a s a -> a
^. Getting Service (Request a) Service
forall a (f :: * -> *).
Functor f =>
(Service -> f Service) -> Request a -> f (Request a)
AWS.request_service
    endpoint :: Endpoint
endpoint = (Service
service Service
-> Getting (Region -> Endpoint) Service (Region -> Endpoint)
-> Region
-> Endpoint
forall s a. s -> Getting a s a -> a
^. Getting (Region -> Endpoint) Service (Region -> Endpoint)
Lens' Service (Region -> Endpoint)
AWS.service_endpoint) (Env -> Region
forall (withAuth :: * -> *). Env' withAuth -> Region
AWS.region Env
env)