Skip to content

Commit

Permalink
improve error message if channel build randomly fails
Browse files Browse the repository at this point in the history
This also improves error message in case input directory does not exist.

Additionally, this prevents an error in case an empty channel is built.

(resolves #6)
  • Loading branch information
memo33 committed Oct 21, 2024
1 parent ad19148 commit 95df071
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

### Changed
- improved error message if channel-build fails randomly in case old files could not be removed


## [0.4.5] - 2024-10-17
### Fixed
Expand Down
25 changes: 19 additions & 6 deletions src/main/scala/sc4pac/ChannelUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,31 @@ object ChannelUtil {

// Finally, we are sure that everything was formatted correctly, so we can
// move the temp folder to its final destination.
os.move.over(tempJsonDir / "metadata", outputDir / "metadata", createFolders = true)
try {
if (packages.nonEmpty) { // otherwise metadata folder does not exist (we still want channel contents file though)
os.move.over(tempJsonDir / "metadata", outputDir / "metadata", createFolders = true)
}
} catch {
case e: java.nio.file.DirectoryNotEmptyException =>
throw new error.FileOpsFailure(
s"""Failed to overwrite folder ${outputDir / "metadata"}. If the problem persists, delete it manually and try again.""")
}
os.move.over(tempJsonDir / JsonRepoUtil.channelContentsFilename, outputDir / JsonRepoUtil.channelContentsFilename, createFolders = true)
System.err.println(s"Successfully wrote channel contents of ${packagesMap.size} packages and assets.")
}

val packagesTask: Task[Seq[JD.PackageAsset]] =
ZIO.foreach(inputDirs) { inputDir =>
ZIO.foreach(os.walk.stream(inputDir).filter(_.last.endsWith(".yaml")).toSeq) { path =>
readAndParsePkgData(path, root = Some(inputDir))
}.map(_.flatten)
}.map(_.flatten)
.mapError(error.YamlFormatIssue(_))
ZIO.ifZIO(ZIO.attemptBlockingIO(os.exists(inputDir)))(
onFalse = ZIO.fail(new error.FileOpsFailure(s"$inputDir does not exist.")),
onTrue = ZIO.foreach(os.walk.stream(inputDir).filter(_.last.endsWith(".yaml")).toSeq) { path =>
readAndParsePkgData(path, root = Some(inputDir))
}.map(_.flatten)
)
}.map(_.flatten).mapError {
case errStr: String => error.YamlFormatIssue(errStr)
case e: java.io.IOException => e
}

// result
ZIO.blocking {
Expand Down
1 change: 1 addition & 0 deletions src/main/scala/sc4pac/cli.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ object Commands {
case abort: error.Sc4pacTimeout => { System.err.println(Array("Operation aborted.", abort.getMessage).mkString(" ")); exit(1) }
case abort: error.Sc4pacNotInteractive => { System.err.println(s"Operation aborted as terminal is non-interactive: ${abort.getMessage}"); exit(1) }
case abort: error.SymlinkCreationFailed => { System.err.println(s"Operation aborted. ${abort.getMessage}"); exit(1) } // channel-build command
case abort: error.FileOpsFailure => { System.err.println(s"Operation aborted. ${abort.getMessage}"); exit(1) } // channel-build command
case abort: error.YamlFormatIssue => { System.err.println(s"Operation aborted. ${abort.getMessage}"); exit(1) } // channel-build command
case e => { e.printStackTrace(); exit(2) }
},
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/sc4pac/error.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ final class NoChannelsAvailable(val title: String, val detail: String) extends j

final class SymlinkCreationFailed(msg: String) extends java.io.IOException(msg) with Sc4pacErr

final class FileOpsFailure(msg: String) extends java.io.IOException(msg) with Sc4pacErr

final class YamlFormatIssue(msg: String) extends java.io.IOException(msg) with Sc4pacErr

0 comments on commit 95df071

Please sign in to comment.