-- | Internal API error type

{-# LANGUAGE RecordWildCards #-}

{-# OPTIONS_HADDOCK hide #-}

module Blockfrost.Types.ApiError
  ( ApiError (..)
  ) where

import Data.Aeson
import Data.Text (Text)
import GHC.Generics

-- | Fancy JSON error returned
-- by the server
data ApiError = ApiError
  { ApiError -> Text
apiError        :: Text
  , ApiError -> Text
apiErrorMessage :: Text
  , ApiError -> Int
apiErrorCode    :: Int
  } deriving (ApiError -> ApiError -> Bool
(ApiError -> ApiError -> Bool)
-> (ApiError -> ApiError -> Bool) -> Eq ApiError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ApiError -> ApiError -> Bool
== :: ApiError -> ApiError -> Bool
$c/= :: ApiError -> ApiError -> Bool
/= :: ApiError -> ApiError -> Bool
Eq, Int -> ApiError -> ShowS
[ApiError] -> ShowS
ApiError -> String
(Int -> ApiError -> ShowS)
-> (ApiError -> String) -> ([ApiError] -> ShowS) -> Show ApiError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ApiError -> ShowS
showsPrec :: Int -> ApiError -> ShowS
$cshow :: ApiError -> String
show :: ApiError -> String
$cshowList :: [ApiError] -> ShowS
showList :: [ApiError] -> ShowS
Show, (forall x. ApiError -> Rep ApiError x)
-> (forall x. Rep ApiError x -> ApiError) -> Generic ApiError
forall x. Rep ApiError x -> ApiError
forall x. ApiError -> Rep ApiError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ApiError -> Rep ApiError x
from :: forall x. ApiError -> Rep ApiError x
$cto :: forall x. Rep ApiError x -> ApiError
to :: forall x. Rep ApiError x -> ApiError
Generic)

instance ToJSON ApiError  where
  toJSON :: ApiError -> Value
toJSON ApiError{Int
Text
apiError :: ApiError -> Text
apiErrorMessage :: ApiError -> Text
apiErrorCode :: ApiError -> Int
apiError :: Text
apiErrorMessage :: Text
apiErrorCode :: Int
..} =
    [Pair] -> Value
object [ Key
"error" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
apiError
           , Key
"message" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Text -> Value
forall a. ToJSON a => a -> Value
toJSON Text
apiErrorMessage
           , Key
"status_code" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Int -> Value
forall a. ToJSON a => a -> Value
toJSON Int
apiErrorCode
           ]

instance FromJSON ApiError  where
  parseJSON :: Value -> Parser ApiError
parseJSON = String -> (Object -> Parser ApiError) -> Value -> Parser ApiError
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"error" ((Object -> Parser ApiError) -> Value -> Parser ApiError)
-> (Object -> Parser ApiError) -> Value -> Parser ApiError
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
    Text
apiError <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"error"
    Text
apiErrorMessage <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"message"
    Int
apiErrorCode <- Object
o Object -> Key -> Parser Int
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"status_code"
    ApiError -> Parser ApiError
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ApiError -> Parser ApiError) -> ApiError -> Parser ApiError
forall a b. (a -> b) -> a -> b
$ ApiError {Int
Text
apiError :: Text
apiErrorMessage :: Text
apiErrorCode :: Int
apiError :: Text
apiErrorMessage :: Text
apiErrorCode :: Int
..}