module Nbparts.Types.Error where
import Commonmark qualified
import Control.Exception qualified as Exception
import Data.Ord qualified as Ord
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Version qualified as Data
import Data.Version qualified as Version
import Data.Yaml qualified as Yaml
import Nbparts.Types.Manifest qualified as Manifest
import Text.Megaparsec qualified as Megaparsec
import Text.Parsec (errorPos)
recommendedNotebookFormat :: (Int, Int)
recommendedNotebookFormat :: (Int, Int)
recommendedNotebookFormat = (Int
4, Int
5)
data NbpartsError = UnpackError UnpackError | PackError PackError
deriving (Int -> NbpartsError -> ShowS
[NbpartsError] -> ShowS
NbpartsError -> String
(Int -> NbpartsError -> ShowS)
-> (NbpartsError -> String)
-> ([NbpartsError] -> ShowS)
-> Show NbpartsError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NbpartsError -> ShowS
showsPrec :: Int -> NbpartsError -> ShowS
$cshow :: NbpartsError -> String
show :: NbpartsError -> String
$cshowList :: [NbpartsError] -> ShowS
showList :: [NbpartsError] -> ShowS
Show, NbpartsError -> NbpartsError -> Bool
(NbpartsError -> NbpartsError -> Bool)
-> (NbpartsError -> NbpartsError -> Bool) -> Eq NbpartsError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NbpartsError -> NbpartsError -> Bool
== :: NbpartsError -> NbpartsError -> Bool
$c/= :: NbpartsError -> NbpartsError -> Bool
/= :: NbpartsError -> NbpartsError -> Bool
Eq)
data UnpackError
= UnpackParseNotebookError Text
| UnpackParseMarkdownError Commonmark.ParseError
| UnpackUnsupportedNotebookFormat (Int, Int)
| UnpackMissingCellIdError
deriving (Int -> UnpackError -> ShowS
[UnpackError] -> ShowS
UnpackError -> String
(Int -> UnpackError -> ShowS)
-> (UnpackError -> String)
-> ([UnpackError] -> ShowS)
-> Show UnpackError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> UnpackError -> ShowS
showsPrec :: Int -> UnpackError -> ShowS
$cshow :: UnpackError -> String
show :: UnpackError -> String
$cshowList :: [UnpackError] -> ShowS
showList :: [UnpackError] -> ShowS
Show, UnpackError -> UnpackError -> Bool
(UnpackError -> UnpackError -> Bool)
-> (UnpackError -> UnpackError -> Bool) -> Eq UnpackError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnpackError -> UnpackError -> Bool
== :: UnpackError -> UnpackError -> Bool
$c/= :: UnpackError -> UnpackError -> Bool
/= :: UnpackError -> UnpackError -> Bool
Eq)
data PackError
= PackUnsupportedNotebookFormat (Int, Int)
| PackParseManifestError ParseYamlError
| PackManifestUnknownVersionError Data.Version
| PackIllegalFormatError IllegalFormatContext Manifest.Format
| PackParseYamlSourcesError ParseYamlError
| PackParseJsonSourcesError Text
| PackParseMarkdownSourcesError (Megaparsec.ParseErrorBundle Text ParseMarkdownSourcesError)
| PackParseYamlMetadataError ParseYamlError
| PackParseJsonMetadataError Text
| PackParseYamlOutputsError ParseYamlError
| PackParseJsonOutputsError Text
| PackMissingCellIdError
| PackCellMetadataTypeMismatch {PackError -> CellMetadataTag
expected :: CellMetadataTag, PackError -> CellMetadataTag
actual :: CellMetadataTag}
deriving (Int -> PackError -> ShowS
[PackError] -> ShowS
PackError -> String
(Int -> PackError -> ShowS)
-> (PackError -> String)
-> ([PackError] -> ShowS)
-> Show PackError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PackError -> ShowS
showsPrec :: Int -> PackError -> ShowS
$cshow :: PackError -> String
show :: PackError -> String
$cshowList :: [PackError] -> ShowS
showList :: [PackError] -> ShowS
Show, PackError -> PackError -> Bool
(PackError -> PackError -> Bool)
-> (PackError -> PackError -> Bool) -> Eq PackError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PackError -> PackError -> Bool
== :: PackError -> PackError -> Bool
$c/= :: PackError -> PackError -> Bool
/= :: PackError -> PackError -> Bool
Eq)
newtype ParseYamlError = ParseYamlError Yaml.ParseException
deriving (Int -> ParseYamlError -> ShowS
[ParseYamlError] -> ShowS
ParseYamlError -> String
(Int -> ParseYamlError -> ShowS)
-> (ParseYamlError -> String)
-> ([ParseYamlError] -> ShowS)
-> Show ParseYamlError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ParseYamlError -> ShowS
showsPrec :: Int -> ParseYamlError -> ShowS
$cshow :: ParseYamlError -> String
show :: ParseYamlError -> String
$cshowList :: [ParseYamlError] -> ShowS
showList :: [ParseYamlError] -> ShowS
Show)
instance Eq ParseYamlError where
ParseYamlError
a == :: ParseYamlError -> ParseYamlError -> Bool
== ParseYamlError
b = ParseYamlError -> String
forall a. Show a => a -> String
show ParseYamlError
a String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== ParseYamlError -> String
forall a. Show a => a -> String
show ParseYamlError
b
data IllegalFormatContext = IllegalFormatSources | IllegalFormatMetadata | IllegalFormatOutputs
deriving (Int -> IllegalFormatContext -> ShowS
[IllegalFormatContext] -> ShowS
IllegalFormatContext -> String
(Int -> IllegalFormatContext -> ShowS)
-> (IllegalFormatContext -> String)
-> ([IllegalFormatContext] -> ShowS)
-> Show IllegalFormatContext
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> IllegalFormatContext -> ShowS
showsPrec :: Int -> IllegalFormatContext -> ShowS
$cshow :: IllegalFormatContext -> String
show :: IllegalFormatContext -> String
$cshowList :: [IllegalFormatContext] -> ShowS
showList :: [IllegalFormatContext] -> ShowS
Show, IllegalFormatContext -> IllegalFormatContext -> Bool
(IllegalFormatContext -> IllegalFormatContext -> Bool)
-> (IllegalFormatContext -> IllegalFormatContext -> Bool)
-> Eq IllegalFormatContext
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IllegalFormatContext -> IllegalFormatContext -> Bool
== :: IllegalFormatContext -> IllegalFormatContext -> Bool
$c/= :: IllegalFormatContext -> IllegalFormatContext -> Bool
/= :: IllegalFormatContext -> IllegalFormatContext -> Bool
Eq, Eq IllegalFormatContext
Eq IllegalFormatContext =>
(IllegalFormatContext -> IllegalFormatContext -> Ordering)
-> (IllegalFormatContext -> IllegalFormatContext -> Bool)
-> (IllegalFormatContext -> IllegalFormatContext -> Bool)
-> (IllegalFormatContext -> IllegalFormatContext -> Bool)
-> (IllegalFormatContext -> IllegalFormatContext -> Bool)
-> (IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext)
-> (IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext)
-> Ord IllegalFormatContext
IllegalFormatContext -> IllegalFormatContext -> Bool
IllegalFormatContext -> IllegalFormatContext -> Ordering
IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: IllegalFormatContext -> IllegalFormatContext -> Ordering
compare :: IllegalFormatContext -> IllegalFormatContext -> Ordering
$c< :: IllegalFormatContext -> IllegalFormatContext -> Bool
< :: IllegalFormatContext -> IllegalFormatContext -> Bool
$c<= :: IllegalFormatContext -> IllegalFormatContext -> Bool
<= :: IllegalFormatContext -> IllegalFormatContext -> Bool
$c> :: IllegalFormatContext -> IllegalFormatContext -> Bool
> :: IllegalFormatContext -> IllegalFormatContext -> Bool
$c>= :: IllegalFormatContext -> IllegalFormatContext -> Bool
>= :: IllegalFormatContext -> IllegalFormatContext -> Bool
$cmax :: IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext
max :: IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext
$cmin :: IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext
min :: IllegalFormatContext
-> IllegalFormatContext -> IllegalFormatContext
Ord)
data ParseMarkdownSourcesError
= ParseMarkdownSourcesJsonError Text
| ParseMarkdownSourcesMarkdownError Commonmark.ParseError
deriving (Int -> ParseMarkdownSourcesError -> ShowS
[ParseMarkdownSourcesError] -> ShowS
ParseMarkdownSourcesError -> String
(Int -> ParseMarkdownSourcesError -> ShowS)
-> (ParseMarkdownSourcesError -> String)
-> ([ParseMarkdownSourcesError] -> ShowS)
-> Show ParseMarkdownSourcesError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ParseMarkdownSourcesError -> ShowS
showsPrec :: Int -> ParseMarkdownSourcesError -> ShowS
$cshow :: ParseMarkdownSourcesError -> String
show :: ParseMarkdownSourcesError -> String
$cshowList :: [ParseMarkdownSourcesError] -> ShowS
showList :: [ParseMarkdownSourcesError] -> ShowS
Show, ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool
(ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool)
-> (ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool)
-> Eq ParseMarkdownSourcesError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool
== :: ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool
$c/= :: ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool
/= :: ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Bool
Eq)
instance Ord ParseMarkdownSourcesError where
compare :: ParseMarkdownSourcesError -> ParseMarkdownSourcesError -> Ordering
compare (ParseMarkdownSourcesMarkdownError ParseError
mdErr1) (ParseMarkdownSourcesMarkdownError ParseError
mdErr2) =
SourcePos -> SourcePos -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (ParseError -> SourcePos
errorPos ParseError
mdErr1) (ParseError -> SourcePos
errorPos ParseError
mdErr2)
compare (ParseMarkdownSourcesJsonError Text
t1) (ParseMarkdownSourcesJsonError Text
t2) = Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Text
t1 Text
t2
compare ParseMarkdownSourcesError
_ ParseMarkdownSourcesError
_ = Ordering
Ord.EQ
data CellMetadataTag = CodeCellMetadataTag | GenericCellMetadataTag
deriving (Int -> CellMetadataTag -> ShowS
[CellMetadataTag] -> ShowS
CellMetadataTag -> String
(Int -> CellMetadataTag -> ShowS)
-> (CellMetadataTag -> String)
-> ([CellMetadataTag] -> ShowS)
-> Show CellMetadataTag
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CellMetadataTag -> ShowS
showsPrec :: Int -> CellMetadataTag -> ShowS
$cshow :: CellMetadataTag -> String
show :: CellMetadataTag -> String
$cshowList :: [CellMetadataTag] -> ShowS
showList :: [CellMetadataTag] -> ShowS
Show, CellMetadataTag -> CellMetadataTag -> Bool
(CellMetadataTag -> CellMetadataTag -> Bool)
-> (CellMetadataTag -> CellMetadataTag -> Bool)
-> Eq CellMetadataTag
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CellMetadataTag -> CellMetadataTag -> Bool
== :: CellMetadataTag -> CellMetadataTag -> Bool
$c/= :: CellMetadataTag -> CellMetadataTag -> Bool
/= :: CellMetadataTag -> CellMetadataTag -> Bool
Eq, Eq CellMetadataTag
Eq CellMetadataTag =>
(CellMetadataTag -> CellMetadataTag -> Ordering)
-> (CellMetadataTag -> CellMetadataTag -> Bool)
-> (CellMetadataTag -> CellMetadataTag -> Bool)
-> (CellMetadataTag -> CellMetadataTag -> Bool)
-> (CellMetadataTag -> CellMetadataTag -> Bool)
-> (CellMetadataTag -> CellMetadataTag -> CellMetadataTag)
-> (CellMetadataTag -> CellMetadataTag -> CellMetadataTag)
-> Ord CellMetadataTag
CellMetadataTag -> CellMetadataTag -> Bool
CellMetadataTag -> CellMetadataTag -> Ordering
CellMetadataTag -> CellMetadataTag -> CellMetadataTag
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: CellMetadataTag -> CellMetadataTag -> Ordering
compare :: CellMetadataTag -> CellMetadataTag -> Ordering
$c< :: CellMetadataTag -> CellMetadataTag -> Bool
< :: CellMetadataTag -> CellMetadataTag -> Bool
$c<= :: CellMetadataTag -> CellMetadataTag -> Bool
<= :: CellMetadataTag -> CellMetadataTag -> Bool
$c> :: CellMetadataTag -> CellMetadataTag -> Bool
> :: CellMetadataTag -> CellMetadataTag -> Bool
$c>= :: CellMetadataTag -> CellMetadataTag -> Bool
>= :: CellMetadataTag -> CellMetadataTag -> Bool
$cmax :: CellMetadataTag -> CellMetadataTag -> CellMetadataTag
max :: CellMetadataTag -> CellMetadataTag -> CellMetadataTag
$cmin :: CellMetadataTag -> CellMetadataTag -> CellMetadataTag
min :: CellMetadataTag -> CellMetadataTag -> CellMetadataTag
Ord)
instance Megaparsec.ShowErrorComponent ParseMarkdownSourcesError where
showErrorComponent :: ParseMarkdownSourcesError -> String
showErrorComponent (ParseMarkdownSourcesJsonError Text
msg) =
String
"Invalid JSON in nbparts cell marker: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
msg
showErrorComponent (ParseMarkdownSourcesMarkdownError ParseError
mdErr) =
String
"Failed to parse markdown: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ParseError -> String
forall a. Show a => a -> String
show ParseError
mdErr
renderError :: NbpartsError -> Text
renderError :: NbpartsError -> Text
renderError NbpartsError
err = case NbpartsError
err of
UnpackError (UnpackParseNotebookError Text
message) -> Text
"Failed to parse notebook: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
message
UnpackError (UnpackParseMarkdownError ParseError
mdErr) -> Text
"Failed to parse markdown: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (ParseError -> String
forall a. Show a => a -> String
show ParseError
mdErr)
UnpackError (UnpackUnsupportedNotebookFormat (Int
major, Int
minor)) ->
Text
"Unsupported notebook format: "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show Int
major)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show Int
minor)
UnpackError UnpackError
UnpackMissingCellIdError ->
Text
"Notebook contains cell(s) without an identifier. Try upgrading your notebook to at least version "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> Int
forall a b. (a, b) -> a
fst (Int, Int)
recommendedNotebookFormat)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ (Int, Int) -> Int
forall a b. (a, b) -> b
snd (Int, Int)
recommendedNotebookFormat)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
PackError (PackUnsupportedNotebookFormat (Int
major, Int
minor)) ->
Text
"Unsupported notebook format: "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show Int
major)
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Int -> String
forall a. Show a => a -> String
show Int
minor)
PackError (PackParseManifestError (ParseYamlError ParseException
ex)) -> Text
"Failed to parse manifest: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (ParseException -> String
forall e. Exception e => e -> String
Exception.displayException ParseException
ex)
PackError (PackManifestUnknownVersionError Version
version) -> Text
"Unknown manifest version: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (Version -> String
Version.showVersion Version
version)
PackError (PackIllegalFormatError IllegalFormatContext
ctx Format
fmt) -> Text
"Illegal format for " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> IllegalFormatContext -> Text
renderIllegalFormatContext IllegalFormatContext
ctx Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
":" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Format -> Text
renderFormat Format
fmt
PackError (PackParseYamlSourcesError (ParseYamlError ParseException
ex)) -> Text
"Failed to parse sources: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (ParseException -> String
forall e. Exception e => e -> String
Exception.displayException ParseException
ex)
PackError (PackParseJsonSourcesError Text
parseErr) -> Text
"Failed to parse sources: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
parseErr
PackError (PackParseMarkdownSourcesError ParseErrorBundle Text ParseMarkdownSourcesError
errBundle) -> String -> Text
Text.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ ParseErrorBundle Text ParseMarkdownSourcesError -> String
forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
Megaparsec.errorBundlePretty ParseErrorBundle Text ParseMarkdownSourcesError
errBundle
PackError (PackParseYamlMetadataError (ParseYamlError ParseException
ex)) -> Text
"Failed to parse metadata: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (ParseException -> String
forall e. Exception e => e -> String
Exception.displayException ParseException
ex)
PackError (PackParseJsonMetadataError Text
parseErr) -> Text
"Failed to parse metadata: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
parseErr
PackError (PackParseYamlOutputsError (ParseYamlError ParseException
ex)) -> Text
"Failed to parse outputs: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack (ParseException -> String
forall e. Exception e => e -> String
Exception.displayException ParseException
ex)
PackError (PackParseJsonOutputsError Text
parseErr) -> Text
"Failed to parse outputs: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
parseErr
PackError PackError
PackMissingCellIdError -> Text
"Markdown content contains missing cell ID"
PackError (PackCellMetadataTypeMismatch CellMetadataTag
expected CellMetadataTag
actual) ->
Text
"Cell metadata type mismatch. Expected: "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> CellMetadataTag -> Text
renderCellMetadataTag CellMetadataTag
expected
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", but got: "
Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> CellMetadataTag -> Text
renderCellMetadataTag CellMetadataTag
actual
renderFormat :: Manifest.Format -> Text
renderFormat :: Format -> Text
renderFormat Format
Manifest.FormatYaml = Text
"yaml"
renderFormat Format
Manifest.FormatJson = Text
"json"
renderFormat Format
Manifest.FormatMarkdown = Text
"markdown"
renderIllegalFormatContext :: IllegalFormatContext -> Text
renderIllegalFormatContext :: IllegalFormatContext -> Text
renderIllegalFormatContext IllegalFormatContext
IllegalFormatSources = Text
"sources"
renderIllegalFormatContext IllegalFormatContext
IllegalFormatMetadata = Text
"metadata"
renderIllegalFormatContext IllegalFormatContext
IllegalFormatOutputs = Text
"outputs"
renderCellMetadataTag :: CellMetadataTag -> Text
renderCellMetadataTag :: CellMetadataTag -> Text
renderCellMetadataTag CellMetadataTag
CodeCellMetadataTag = Text
"code cell metadata"
renderCellMetadataTag CellMetadataTag
GenericCellMetadataTag = Text
"generic cell metadata"