-- |
-- Module      : Amazonka.Auth.Exception
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
--
-- Exception for errors involving AWS authentication.
module Amazonka.Auth.Exception where

import Amazonka.Core.Lens.Internal (exception, prism)
import Amazonka.Data
import Amazonka.Prelude
import Amazonka.Types

-- | An error thrown when attempting to read AuthN/AuthZ information.
data AuthError
  = RetrievalError HttpException
  | MissingEnvError Text
  | MissingFileError FilePath
  | InvalidFileError Text
  | InvalidIAMError Text
  | CredentialChainExhausted
  deriving stock (Int -> AuthError -> ShowS
[AuthError] -> ShowS
AuthError -> String
(Int -> AuthError -> ShowS)
-> (AuthError -> String)
-> ([AuthError] -> ShowS)
-> Show AuthError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AuthError -> ShowS
showsPrec :: Int -> AuthError -> ShowS
$cshow :: AuthError -> String
show :: AuthError -> String
$cshowList :: [AuthError] -> ShowS
showList :: [AuthError] -> ShowS
Show, (forall x. AuthError -> Rep AuthError x)
-> (forall x. Rep AuthError x -> AuthError) -> Generic AuthError
forall x. Rep AuthError x -> AuthError
forall x. AuthError -> Rep AuthError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AuthError -> Rep AuthError x
from :: forall x. AuthError -> Rep AuthError x
$cto :: forall x. Rep AuthError x -> AuthError
to :: forall x. Rep AuthError x -> AuthError
Generic)

instance Exception AuthError

instance ToLog AuthError where
  build :: AuthError -> ByteStringBuilder
build = \case
    RetrievalError HttpException
e -> HttpException -> ByteStringBuilder
forall a. ToLog a => a -> ByteStringBuilder
build HttpException
e
    MissingEnvError Text
e -> ByteStringBuilder
"[MissingEnvError]  { message = " ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> Text -> ByteStringBuilder
forall a. ToLog a => a -> ByteStringBuilder
build Text
e ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> ByteStringBuilder
"}"
    MissingFileError String
f -> ByteStringBuilder
"[MissingFileError] { path = " ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> String -> ByteStringBuilder
forall a. ToLog a => a -> ByteStringBuilder
build String
f ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> ByteStringBuilder
"}"
    InvalidFileError Text
e -> ByteStringBuilder
"[InvalidFileError] { message = " ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> Text -> ByteStringBuilder
forall a. ToLog a => a -> ByteStringBuilder
build Text
e ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> ByteStringBuilder
"}"
    InvalidIAMError Text
e -> ByteStringBuilder
"[InvalidIAMError]  { message = " ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> Text -> ByteStringBuilder
forall a. ToLog a => a -> ByteStringBuilder
build Text
e ByteStringBuilder -> ByteStringBuilder -> ByteStringBuilder
forall a. Semigroup a => a -> a -> a
<> ByteStringBuilder
"}"
    AuthError
CredentialChainExhausted -> ByteStringBuilder
"[CredentialChainExhausted]"

class AsAuthError a where
  -- | A general authentication error.
  _AuthError :: Prism' a AuthError

  {-# MINIMAL _AuthError #-}

  -- | An error occured while communicating over HTTP with
  -- the local metadata endpoint.
  _RetrievalError :: Prism' a HttpException

  -- | The named environment variable was not found.
  _MissingEnvError :: Prism' a Text

  -- | The specified credentials file could not be found.
  _MissingFileError :: Prism' a FilePath

  -- | An error occured parsing the credentials file.
  _InvalidFileError :: Prism' a Text

  -- | The specified IAM profile could not be found or deserialised.
  _InvalidIAMError :: Prism' a Text

  _RetrievalError = p AuthError (f AuthError) -> p a (f a)
forall a. AsAuthError a => Prism' a AuthError
Prism' a AuthError
_AuthError (p AuthError (f AuthError) -> p a (f a))
-> (p HttpException (f HttpException) -> p AuthError (f AuthError))
-> p HttpException (f HttpException)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p HttpException (f HttpException) -> p AuthError (f AuthError)
forall a. AsAuthError a => Prism' a HttpException
Prism' AuthError HttpException
_RetrievalError
  _MissingEnvError = p AuthError (f AuthError) -> p a (f a)
forall a. AsAuthError a => Prism' a AuthError
Prism' a AuthError
_AuthError (p AuthError (f AuthError) -> p a (f a))
-> (p Text (f Text) -> p AuthError (f AuthError))
-> p Text (f Text)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Text (f Text) -> p AuthError (f AuthError)
forall a. AsAuthError a => Prism' a Text
Prism' AuthError Text
_MissingEnvError
  _MissingFileError = p AuthError (f AuthError) -> p a (f a)
forall a. AsAuthError a => Prism' a AuthError
Prism' a AuthError
_AuthError (p AuthError (f AuthError) -> p a (f a))
-> (p String (f String) -> p AuthError (f AuthError))
-> p String (f String)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p String (f String) -> p AuthError (f AuthError)
forall a. AsAuthError a => Prism' a String
Prism' AuthError String
_MissingFileError
  _InvalidFileError = p AuthError (f AuthError) -> p a (f a)
forall a. AsAuthError a => Prism' a AuthError
Prism' a AuthError
_AuthError (p AuthError (f AuthError) -> p a (f a))
-> (p Text (f Text) -> p AuthError (f AuthError))
-> p Text (f Text)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Text (f Text) -> p AuthError (f AuthError)
forall a. AsAuthError a => Prism' a Text
Prism' AuthError Text
_InvalidFileError
  _InvalidIAMError = p AuthError (f AuthError) -> p a (f a)
forall a. AsAuthError a => Prism' a AuthError
Prism' a AuthError
_AuthError (p AuthError (f AuthError) -> p a (f a))
-> (p Text (f Text) -> p AuthError (f AuthError))
-> p Text (f Text)
-> p a (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. p Text (f Text) -> p AuthError (f AuthError)
forall a. AsAuthError a => Prism' a Text
Prism' AuthError Text
_InvalidIAMError

instance AsAuthError SomeException where
  _AuthError :: Prism' SomeException AuthError
_AuthError = p AuthError (f AuthError) -> p SomeException (f SomeException)
forall a. Exception a => Prism' SomeException a
Prism' SomeException AuthError
exception

instance AsAuthError AuthError where
  _AuthError :: Prism' AuthError AuthError
_AuthError = p AuthError (f AuthError) -> p AuthError (f AuthError)
forall a. a -> a
id

  _RetrievalError :: Prism' AuthError HttpException
_RetrievalError = (HttpException -> AuthError)
-> (AuthError -> Either AuthError HttpException)
-> Prism' AuthError HttpException
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism HttpException -> AuthError
RetrievalError ((AuthError -> Either AuthError HttpException)
 -> Prism' AuthError HttpException)
-> (AuthError -> Either AuthError HttpException)
-> Prism' AuthError HttpException
forall a b. (a -> b) -> a -> b
$ \case
    RetrievalError HttpException
e -> HttpException -> Either AuthError HttpException
forall a b. b -> Either a b
Right HttpException
e
    AuthError
x -> AuthError -> Either AuthError HttpException
forall a b. a -> Either a b
Left AuthError
x

  _MissingEnvError :: Prism' AuthError Text
_MissingEnvError = (Text -> AuthError)
-> (AuthError -> Either AuthError Text) -> Prism' AuthError Text
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Text -> AuthError
MissingEnvError ((AuthError -> Either AuthError Text) -> Prism' AuthError Text)
-> (AuthError -> Either AuthError Text) -> Prism' AuthError Text
forall a b. (a -> b) -> a -> b
$ \case
    MissingEnvError Text
e -> Text -> Either AuthError Text
forall a b. b -> Either a b
Right Text
e
    AuthError
x -> AuthError -> Either AuthError Text
forall a b. a -> Either a b
Left AuthError
x

  _MissingFileError :: Prism' AuthError String
_MissingFileError = (String -> AuthError)
-> (AuthError -> Either AuthError String)
-> Prism' AuthError String
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism String -> AuthError
MissingFileError ((AuthError -> Either AuthError String) -> Prism' AuthError String)
-> (AuthError -> Either AuthError String)
-> Prism' AuthError String
forall a b. (a -> b) -> a -> b
$ \case
    MissingFileError String
f -> String -> Either AuthError String
forall a b. b -> Either a b
Right String
f
    AuthError
x -> AuthError -> Either AuthError String
forall a b. a -> Either a b
Left AuthError
x

  _InvalidFileError :: Prism' AuthError Text
_InvalidFileError = (Text -> AuthError)
-> (AuthError -> Either AuthError Text) -> Prism' AuthError Text
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Text -> AuthError
InvalidFileError ((AuthError -> Either AuthError Text) -> Prism' AuthError Text)
-> (AuthError -> Either AuthError Text) -> Prism' AuthError Text
forall a b. (a -> b) -> a -> b
$ \case
    InvalidFileError Text
e -> Text -> Either AuthError Text
forall a b. b -> Either a b
Right Text
e
    AuthError
x -> AuthError -> Either AuthError Text
forall a b. a -> Either a b
Left AuthError
x

  _InvalidIAMError :: Prism' AuthError Text
_InvalidIAMError = (Text -> AuthError)
-> (AuthError -> Either AuthError Text) -> Prism' AuthError Text
forall b t s a. (b -> t) -> (s -> Either t a) -> Prism s t a b
prism Text -> AuthError
InvalidIAMError ((AuthError -> Either AuthError Text) -> Prism' AuthError Text)
-> (AuthError -> Either AuthError Text) -> Prism' AuthError Text
forall a b. (a -> b) -> a -> b
$ \case
    InvalidIAMError Text
e -> Text -> Either AuthError Text
forall a b. b -> Either a b
Right Text
e
    AuthError
x -> AuthError -> Either AuthError Text
forall a b. a -> Either a b
Left AuthError
x