-
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 3 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,58 @@ | ||
| package eu.neverblink.jelly.cli | ||
|
|
||
| case class InputFileNotFound(file: String) | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 permission to create output file $file in this directory.", | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) | ||
| case class JellyDeserializationError(message: String) | ||
| extends ParsingError(s"Jelly deserialization error: $message.") | ||
| case class JellySerializationError(message: String) | ||
| extends ParsingError(s"Jelly serialization error: $message.") | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case class JellyTranscodingError(message: String) | ||
| extends ParsingError(s"Jelly transcoding error: $message.") | ||
| class ParsingError(message: String) extends CriticalException(s"Parsing error: $message.") | ||
| case class InputOutputTranslationLossy(format: String) | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| extends NonCriticalException( | ||
| "Input file cannot be fully translated to output format. The translation will be lossy.", | ||
| ) | ||
| case class ExitException(code: Int) extends CriticalException(s"Exiting with code $code.") | ||
|
|
||
| class CriticalException(message: String) extends Exception(message) | ||
| class NonCriticalException(message: String) extends Exception(message) | ||
|
|
||
| /** Handle exceptions. Critical exceptions will exit the application with code 1. Non-critical | ||
| * exceptions will be printed to std err. | ||
| */ | ||
|
|
||
| object ErrorHandler: | ||
|
|
||
| def handle(command: JellyCommand[?], t: Throwable): Unit = | ||
| t match | ||
| case e: ParsingError => | ||
| command.printLine(f"${e.getMessage}", toStderr = true) | ||
| printStackTrace(command, e) | ||
| command.exit(1) | ||
| case e: CriticalException => | ||
| command.printLine(f"${e.getMessage}", toStderr = true) | ||
| command.exit(1) | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case e: NonCriticalException => | ||
| command.printLine(f"${e.getMessage}", toStderr = true) | ||
| case e: Throwable => | ||
| command.printLine("Unknown error", toStderr = true) | ||
| printStackTrace(command, e) | ||
| command.exit(1) | ||
|
|
||
| /** Print out stack trace or debugging information | ||
| * @param command | ||
| * @param t | ||
| */ | ||
| private def printStackTrace( | ||
| command: JellyCommand[?], | ||
| t: Throwable, | ||
| ): Unit = | ||
| if App.debugMode then t.printStackTrace(command.err) | ||
| else command.printLine("If needed, run with --debug to see the stack trace.", toStderr = true) | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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
35 changes: 35 additions & 0 deletions
35
src/main/scala/eu/neverblink/jelly/cli/command/rdf/Ops.scala
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.command.rdf | ||
|
|
||
| import eu.neverblink.jelly.cli.{InputFileInaccessible, InputFileNotFound, OutputFileCannotBeCreated} | ||
|
|
||
| import java.io.{File, FileInputStream, FileOutputStream} | ||
|
|
||
| /** Object for small, repeating operations with proper error handling | ||
| */ | ||
| object Ops: | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** Read input file and return FileInputStream | ||
| * @param fileName | ||
| * @throws InputFileNotFound | ||
| * @throws InputFileInaccessible | ||
| * @return | ||
| * FileInputStream | ||
| */ | ||
| def readInputFile(fileName: String): FileInputStream = | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 createOutputStream(fileName: String): FileOutputStream = | ||
Karolina-Bogacka marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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) | ||
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
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.