{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Types.Errors.ScriptDataError
  ( ScriptDataError (..)
  , renderScriptDataError
  )
where

import           Cardano.Api

import qualified Cardano.Binary as CBOR

data ScriptDataError
  = ScriptDataErrorFile (FileError ())
  | ScriptDataErrorJsonParse !FilePath !String
  | ScriptDataErrorConversion !FilePath !ScriptDataJsonError
  | ScriptDataErrorValidation !FilePath !ScriptDataRangeError
  | ScriptDataErrorMetadataDecode !FilePath !CBOR.DecoderError
  | ScriptDataErrorJsonBytes !ScriptDataJsonBytesError
  deriving Int -> ScriptDataError -> ShowS
[ScriptDataError] -> ShowS
ScriptDataError -> String
(Int -> ScriptDataError -> ShowS)
-> (ScriptDataError -> String)
-> ([ScriptDataError] -> ShowS)
-> Show ScriptDataError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptDataError -> ShowS
showsPrec :: Int -> ScriptDataError -> ShowS
$cshow :: ScriptDataError -> String
show :: ScriptDataError -> String
$cshowList :: [ScriptDataError] -> ShowS
showList :: [ScriptDataError] -> ShowS
Show

renderScriptDataError :: ScriptDataError -> Doc ann
renderScriptDataError :: forall ann. ScriptDataError -> Doc ann
renderScriptDataError = \case
  ScriptDataErrorFile FileError ()
err ->
    FileError () -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. FileError () -> Doc ann
prettyError FileError ()
err
  ScriptDataErrorJsonParse String
fp String
jsonErr ->
    Doc ann
"Invalid JSON format in file:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow String
fp Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"\nJSON parse error:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall ann. String -> Doc ann
forall a ann. Pretty a => a -> Doc ann
pretty String
jsonErr
  ScriptDataErrorConversion String
fp ScriptDataJsonError
sDataJsonErr ->
    Doc ann
"Error reading metadata at:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow String
fp Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
"\n" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> ScriptDataJsonError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. ScriptDataJsonError -> Doc ann
prettyError ScriptDataJsonError
sDataJsonErr
  ScriptDataErrorValidation String
fp ScriptDataRangeError
sDataRangeErr ->
    Doc ann
"Error validating script data at:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow String
fp Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> Doc ann
":\n" Doc ann -> Doc ann -> Doc ann
forall a. Semigroup a => a -> a -> a
<> ScriptDataRangeError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. ScriptDataRangeError -> Doc ann
prettyError ScriptDataRangeError
sDataRangeErr
  ScriptDataErrorMetadataDecode String
fp DecoderError
decoderErr ->
    Doc ann
"Error decoding CBOR metadata at:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> String -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow String
fp Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ann
"Error:" Doc ann -> Doc ann -> Doc ann
forall ann. Doc ann -> Doc ann -> Doc ann
<+> DecoderError -> Doc ann
forall a ann. Show a => a -> Doc ann
pshow DecoderError
decoderErr
  ScriptDataErrorJsonBytes ScriptDataJsonBytesError
e ->
    ScriptDataJsonBytesError -> Doc ann
forall e ann. Error e => e -> Doc ann
forall ann. ScriptDataJsonBytesError -> Doc ann
prettyError ScriptDataJsonBytesError
e