-
Notifications
You must be signed in to change notification settings - Fork 2
Add better error handling #35
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a839afb
Add basic skeleton for error nadling
Karolina-Bogacka 0ffb66b
Add error handling tests
Karolina-Bogacka dcad7e7
Change the truncation paradigm
Karolina-Bogacka 625b9a8
Save applied updates
Karolina-Bogacka 6e9afcd
Correct comments raised in review
Karolina-Bogacka b53b60c
Change exception wording a little
Karolina-Bogacka 9bba808
Make a bit nicer comments
Karolina-Bogacka 462b0b1
Correct review comments
Karolina-Bogacka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package eu.neverblink.jelly.cli | ||
|
|
||
| /** Handle exceptions. Common critical exceptions will be given custom output messages. | ||
| */ | ||
|
|
||
| object ErrorHandler: | ||
|
|
||
| def handle(command: JellyCommand[?], t: Throwable): Unit = | ||
| t match | ||
| case e: CriticalException => | ||
| command.printLine(f"${e.getMessage}", toStderr = true) | ||
| case e: Throwable => | ||
| command.printLine("Unknown error", toStderr = true) | ||
| printStackTrace(command, t) | ||
| command.exit(1) | ||
|
|
||
| /** Print out stack trace or debugging information | ||
| * @param command | ||
| * @param t | ||
| */ | ||
| private def printStackTrace( | ||
| command: JellyCommand[?], | ||
| t: Throwable, | ||
| ): Unit = | ||
| if command.isDebugMode then t.printStackTrace(command.err) | ||
| else command.printLine("Run with --debug to see the complete stack trace.", toStderr = true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package eu.neverblink.jelly.cli | ||
|
|
||
| import com.google.protobuf.InvalidProtocolBufferException | ||
| import org.apache.jena.riot.RiotException | ||
|
|
||
| /** Contains a set of common jelly-cli exceptions with custom output messages. | ||
| */ | ||
|
|
||
| case class InputFileNotFound(file: String) | ||
| extends CriticalException(s"Input file $file does not exist.") | ||
| case class InputFileInaccessible(file: String) | ||
| extends CriticalException(s"Not enough permissions to read input file $file.") | ||
| case class OutputFileCannotBeCreated(file: String) | ||
| extends CriticalException( | ||
| s"Not enough permissions to create output file $file in this directory.", | ||
| ) | ||
| case class JellyDeserializationError(message: String) | ||
| extends CriticalException(s"Jelly deserialization error: $message") | ||
| case class JellySerializationError(message: String) | ||
| extends CriticalException(s"Jelly serialization error: $message") | ||
| case class JellyTranscodingError(message: String) | ||
| extends CriticalException(s"Jelly transcoding error: $message") | ||
| case class JenaRiotException(e: RiotException) | ||
| extends CriticalException(s"Riot exception: ${e.getMessage}") | ||
| case class InvalidJellyFile(e: InvalidProtocolBufferException) | ||
| extends CriticalException(s"Invalid Jelly file: ${e.getMessage}") | ||
| case class ExitException(code: Int) extends CriticalException(s"Exiting with code $code.") | ||
|
|
||
| class CriticalException(message: String) extends Exception(message) | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/main/scala/eu/neverblink/jelly/cli/command/FoolAround.scala
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package eu.neverblink.jelly.cli.util | ||
|
|
||
| import eu.neverblink.jelly.cli.{InputFileInaccessible, InputFileNotFound, OutputFileCannotBeCreated} | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| import java.io.{File, FileInputStream, FileOutputStream} | ||
|
|
||
| /** Object for small, repeating I/O operations | ||
| */ | ||
| object IoUtil: | ||
|
|
||
| /** Read input file and return FileInputStream | ||
| * @param fileName | ||
| * @throws InputFileNotFound | ||
| * @throws InputFileInaccessible | ||
| * @return | ||
| * FileInputStream | ||
| */ | ||
| def inputStream(fileName: String): FileInputStream = | ||
| val file = File(fileName) | ||
| if !file.exists then throw InputFileNotFound(fileName) | ||
| if !file.canRead then throw InputFileInaccessible(fileName) | ||
| FileInputStream(file) | ||
|
|
||
| /** Create output stream with extra error handling. If the file exists, it will append to it. | ||
| * @param fileName | ||
| * @throws OutputFileExists | ||
| * @return | ||
| * FileOutputStream | ||
| */ | ||
| def outputStream(fileName: String): FileOutputStream = | ||
| val file = File(fileName) | ||
| val suppFile = file.getParentFile | ||
| val parentFile = if (suppFile != null) suppFile else File(".") | ||
| if !parentFile.canWrite then throw OutputFileCannotBeCreated(fileName) | ||
| FileOutputStream(file, true) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.