Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move parse + typecheck failures from LoadingFailure to SystemFailure #2323

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/swarm-scenario/Swarm/Game/Entity.hs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ validateEntityAttrRefs validAttrs es =
AWorld n ->
unless (Set.member (WorldAttr $ T.unpack n) validAttrs)
. throwError
. CustomMessage
. SystemFailure
. CustomFailure
$ T.unwords
[ "Nonexistent attribute"
, quote n
Expand All @@ -509,7 +510,7 @@ buildEntityMap es = do
forM_ (findDup $ map fst namedEntities) $
throwError . Duplicate Entities
case combineEntityCapsM entsByName es of
Left x -> throwError $ CustomMessage x
Left x -> throwError . SystemFailure . CustomFailure $ x
Right ebc ->
return $
EntityMap
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-scenario/Swarm/Game/Recipe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ loadRecipes em = do
withThrow (AssetNotLoaded (Data Recipes) fileName . CanNotParseYaml)
. (liftEither <=< sendIO)
$ decodeFileEither @[Recipe Text] fileName
withThrow (AssetNotLoaded (Data Recipes) fileName . CustomMessage)
withThrow (AssetNotLoaded (Data Recipes) fileName . SystemFailure . CustomFailure)
. liftEither
. left (T.append "Unknown entities in recipe(s): " . T.intercalate ", ")
. validationToEither
Expand Down
3 changes: 2 additions & 1 deletion src/swarm-scenario/Swarm/Game/Terrain.hs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ validateTerrainAttrRefs validAttrs rawTerrains =
forM rawTerrains $ \(TerrainItem n a d) -> do
unless (Set.member (WorldAttr $ T.unpack a) validAttrs)
. throwError
. CustomMessage
. SystemFailure
. CustomFailure
$ T.unwords
[ "Nonexistent attribute"
, quote a
Expand Down
4 changes: 2 additions & 2 deletions src/swarm-scenario/Swarm/Game/World/Load.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ loadWorld ::
m (Text, Some (TTerm '[]))
loadWorld dir tem (fp, src) = do
wexp <-
liftEither . left (AssetNotLoaded (Data Worlds) fp . CanNotParseMegaparsec) $
liftEither . left (AssetNotLoaded (Data Worlds) fp . SystemFailure . CanNotParseMegaparsec) $
runParser parseWExp (into @Text src)
t <-
withThrow (AssetNotLoaded (Data Worlds) fp . DoesNotTypecheck . prettyText @CheckErr) $
withThrow (AssetNotLoaded (Data Worlds) fp . SystemFailure . DoesNotTypecheck . prettyText @CheckErr) $
runReader tem . runReader @WorldMap M.empty $
infer CNil wexp
return (into @Text (dropExtension (stripDir dir fp)), t)
Expand Down
2 changes: 1 addition & 1 deletion src/swarm-tui/Swarm/TUI/Model/KeyBindings.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ loadKeybindingConfig = do
else do
loadedCustomBindings <- sendIO $ keybindingsFromFile swarmEvents "keybindings" ini
case loadedCustomBindings of
Left e -> throwError $ AssetNotLoaded Keybindings ini (CustomMessage $ T.pack e)
Left e -> throwError $ AssetNotLoaded Keybindings ini (SystemFailure . CustomFailure $ T.pack e)
Right bs -> pure $ fromMaybe [] bs

initKeyHandlingState ::
Expand Down
26 changes: 13 additions & 13 deletions src/swarm-util/Swarm/Failure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ data LoadingFailure
= DoesNotExist Entry
| EntryNot Entry
| CanNotParseYaml ParseException
| CanNotParseMegaparsec (ParseErrorBundle Text Void)
| DoesNotTypecheck Text -- See Note [Typechecking errors]
| Duplicate AssetData Text
| CustomMessage Text
| SystemFailure SystemFailure
deriving (Show)

-- ~~~~ Note [Pretty-printing typechecking errors]
Expand All @@ -79,6 +77,8 @@ data SystemFailure
= AssetNotLoaded Asset FilePath LoadingFailure
| ScenarioNotFound FilePath
| OrderFileWarning FilePath OrderFileWarning
| CanNotParseMegaparsec (ParseErrorBundle Text Void)
| DoesNotTypecheck Text -- See Note [Typechecking errors]
| CustomFailure Text
deriving (Show)

Expand Down Expand Up @@ -106,23 +106,15 @@ instance PrettyPrec Entry where
prettyPrec _ = prettyShowLow

instance PrettyPrec LoadingFailure where
prettyPrec _ = \case
prettyPrec prec = \case
DoesNotExist e -> "The" <+> ppr e <+> "is missing!"
EntryNot e -> "The entry is not a" <+> ppr e <> "!"
CanNotParseYaml p ->
nest 2 . vcat $
"Parse failure:"
: map pretty (T.lines (into @Text (prettyPrintParseException p)))
CanNotParseMegaparsec p ->
nest 2 . vcat $
"Parse failure:"
: map pretty (T.lines (into @Text (errorBundlePretty p)))
DoesNotTypecheck t ->
nest 2 . vcat $
"Parse failure:"
: map pretty (T.lines t)
Duplicate thing duped -> "Duplicate" <+> ppr thing <> ":" <+> squotes (pretty duped)
CustomMessage m -> pretty m
SystemFailure g -> prettyPrec prec g

instance PrettyPrec OrderFileWarning where
prettyPrec _ = \case
Expand All @@ -147,4 +139,12 @@ instance PrettyPrec SystemFailure where
[ "Warning: while processing" <+> pretty orderFile <> ":"
, ppr w
]
CanNotParseMegaparsec p ->
nest 2 . vcat $
"Parse failure:"
: map pretty (T.lines (into @Text (errorBundlePretty p)))
DoesNotTypecheck t ->
nest 2 . vcat $
"Parse failure:"
: map pretty (T.lines t)
CustomFailure m -> pretty m
2 changes: 1 addition & 1 deletion src/swarm-util/Swarm/ResourceLoading.hs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ readAppData = do
dirMembers :: [FilePath] <-
(liftEither <=< sendIO) $
(pure <$> listDirectory d) `catch` \(e :: IOException) ->
return . Left . AssetNotLoaded (Data AppAsset) d . CustomMessage . T.pack $ show e
return . Left . AssetNotLoaded (Data AppAsset) d . SystemFailure . CustomFailure . T.pack $ show e
let fs = filter ((== ".txt") . takeExtension) dirMembers

filesList <- sendIO $ forM fs (\f -> (into @Text (dropExtension f),) <$> readFileMayT (d </> f))
Expand Down