-- | All the errors that the runtime can throw
module Aws.Lambda.Runtime.Error
  ( EnvironmentVariableNotSet (..),
    Parsing (..),
    Invocation (..),
  )
where

import Control.Exception.Safe.Checked (Exception)
import Data.Aeson (ToJSON (..), Value, object, (.=))
import Data.Text (Text)

newtype EnvironmentVariableNotSet
  = EnvironmentVariableNotSet Text
  deriving (Int -> EnvironmentVariableNotSet -> ShowS
[EnvironmentVariableNotSet] -> ShowS
EnvironmentVariableNotSet -> String
(Int -> EnvironmentVariableNotSet -> ShowS)
-> (EnvironmentVariableNotSet -> String)
-> ([EnvironmentVariableNotSet] -> ShowS)
-> Show EnvironmentVariableNotSet
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EnvironmentVariableNotSet] -> ShowS
$cshowList :: [EnvironmentVariableNotSet] -> ShowS
show :: EnvironmentVariableNotSet -> String
$cshow :: EnvironmentVariableNotSet -> String
showsPrec :: Int -> EnvironmentVariableNotSet -> ShowS
$cshowsPrec :: Int -> EnvironmentVariableNotSet -> ShowS
Show, Show EnvironmentVariableNotSet
Typeable EnvironmentVariableNotSet
Typeable EnvironmentVariableNotSet
-> Show EnvironmentVariableNotSet
-> (EnvironmentVariableNotSet -> SomeException)
-> (SomeException -> Maybe EnvironmentVariableNotSet)
-> (EnvironmentVariableNotSet -> String)
-> Exception EnvironmentVariableNotSet
SomeException -> Maybe EnvironmentVariableNotSet
EnvironmentVariableNotSet -> String
EnvironmentVariableNotSet -> SomeException
forall e.
Typeable e
-> Show e
-> (e -> SomeException)
-> (SomeException -> Maybe e)
-> (e -> String)
-> Exception e
displayException :: EnvironmentVariableNotSet -> String
$cdisplayException :: EnvironmentVariableNotSet -> String
fromException :: SomeException -> Maybe EnvironmentVariableNotSet
$cfromException :: SomeException -> Maybe EnvironmentVariableNotSet
toException :: EnvironmentVariableNotSet -> SomeException
$ctoException :: EnvironmentVariableNotSet -> SomeException
$cp2Exception :: Show EnvironmentVariableNotSet
$cp1Exception :: Typeable EnvironmentVariableNotSet
Exception)

instance ToJSON EnvironmentVariableNotSet where
  toJSON :: EnvironmentVariableNotSet -> Value
toJSON (EnvironmentVariableNotSet Text
msg) =
    [Pair] -> Value
object
      [ Text
"errorType" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
"EnvironmentVariableNotSet" :: Text),
        Text
"errorMessage" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text
msg
      ]

data Parsing = Parsing
  { Parsing -> Text
errorMessage :: Text,
    Parsing -> Text
actualValue :: Text,
    Parsing -> Text
valueName :: Text
  }
  deriving (Int -> Parsing -> ShowS
[Parsing] -> ShowS
Parsing -> String
(Int -> Parsing -> ShowS)
-> (Parsing -> String) -> ([Parsing] -> ShowS) -> Show Parsing
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Parsing] -> ShowS
$cshowList :: [Parsing] -> ShowS
show :: Parsing -> String
$cshow :: Parsing -> String
showsPrec :: Int -> Parsing -> ShowS
$cshowsPrec :: Int -> Parsing -> ShowS
Show, Show Parsing
Typeable Parsing
Typeable Parsing
-> Show Parsing
-> (Parsing -> SomeException)
-> (SomeException -> Maybe Parsing)
-> (Parsing -> String)
-> Exception Parsing
SomeException -> Maybe Parsing
Parsing -> String
Parsing -> SomeException
forall e.
Typeable e
-> Show e
-> (e -> SomeException)
-> (SomeException -> Maybe e)
-> (e -> String)
-> Exception e
displayException :: Parsing -> String
$cdisplayException :: Parsing -> String
fromException :: SomeException -> Maybe Parsing
$cfromException :: SomeException -> Maybe Parsing
toException :: Parsing -> SomeException
$ctoException :: Parsing -> SomeException
$cp2Exception :: Show Parsing
$cp1Exception :: Typeable Parsing
Exception)

instance ToJSON Parsing where
  toJSON :: Parsing -> Value
toJSON (Parsing Text
errorMessage Text
_ Text
valueName) =
    [Pair] -> Value
object
      [ Text
"errorType" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
"Parsing" :: Text),
        Text
"errorMessage" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= (Text
"Could not parse '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
valueName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"': " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
errorMessage)
      ]

newtype Invocation
  = Invocation Value
  deriving (Int -> Invocation -> ShowS
[Invocation] -> ShowS
Invocation -> String
(Int -> Invocation -> ShowS)
-> (Invocation -> String)
-> ([Invocation] -> ShowS)
-> Show Invocation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Invocation] -> ShowS
$cshowList :: [Invocation] -> ShowS
show :: Invocation -> String
$cshow :: Invocation -> String
showsPrec :: Int -> Invocation -> ShowS
$cshowsPrec :: Int -> Invocation -> ShowS
Show, Show Invocation
Typeable Invocation
Typeable Invocation
-> Show Invocation
-> (Invocation -> SomeException)
-> (SomeException -> Maybe Invocation)
-> (Invocation -> String)
-> Exception Invocation
SomeException -> Maybe Invocation
Invocation -> String
Invocation -> SomeException
forall e.
Typeable e
-> Show e
-> (e -> SomeException)
-> (SomeException -> Maybe e)
-> (e -> String)
-> Exception e
displayException :: Invocation -> String
$cdisplayException :: Invocation -> String
fromException :: SomeException -> Maybe Invocation
$cfromException :: SomeException -> Maybe Invocation
toException :: Invocation -> SomeException
$ctoException :: Invocation -> SomeException
$cp2Exception :: Show Invocation
$cp1Exception :: Typeable Invocation
Exception)

instance ToJSON Invocation where
  -- We return the user error as it is
  toJSON :: Invocation -> Value
toJSON (Invocation Value
err) = Value
err