-
Notifications
You must be signed in to change notification settings - Fork 39
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
Add error handling for file loading #353
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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,30 @@ | ||
This is a suite of blackbox integration tests for the `tntc` executable. | ||
The tests in this file check that particular output is produced when | ||
particular input is received. | ||
|
||
These tests require [`txm`](https://www.npmjs.com/package/txm) to be installed: | ||
|
||
```sh | ||
npm install -g txm | ||
``` | ||
|
||
All of the test inputs in the following test cases are commands executed by `bash`. | ||
|
||
<!-- !test program | ||
bash - | ||
--> | ||
|
||
### User error on parse with non-existent file | ||
|
||
Regression test for [#215](https://github.com/informalsystems/tnt/issues/215). | ||
We want to ensure we do not throw uncaught exceptions when the input file is | ||
doesn't exist. | ||
|
||
<!-- !test in non-existent file --> | ||
tntc parse ../examples/non-existent.file | ||
|
||
<!-- !test exit 1 --> | ||
<!-- !test err non-existent file --> | ||
error: file ../examples/non-existent.file does not exist | ||
|
||
|
This file contains 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 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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
* @author Igor Konnov, Gabriela Moreira, Informal Systems, 2021-2022 | ||
*/ | ||
|
||
import { readFileSync, writeFileSync } from 'fs' | ||
import { existsSync, PathLike, readFileSync, writeFileSync } from 'fs' | ||
import { resolve } from 'path' | ||
import { cwd } from 'process' | ||
import { lf } from 'eol' | ||
|
@@ -29,6 +29,7 @@ import { tntRepl } from './repl' | |
import { inferTypes } from './types/inferrer' | ||
import { effectToString } from './effects/printing' | ||
import { typeSchemeToString } from './types/printing' | ||
import { Either, right, left } from '@sweet-monads/either' | ||
|
||
/** | ||
* Parse a TNT specification. | ||
|
@@ -103,18 +104,36 @@ function runRepl(_argv: any) { | |
tntRepl(process.stdin, process.stdout) | ||
} | ||
|
||
// Load a file into a string | ||
function loadFile(p: PathLike): Either<string, string> { | ||
if (existsSync(p)) { | ||
try { | ||
return right(readFileSync(p, 'utf8')) | ||
} catch (err: unknown) { | ||
return left(`error: file ${p} could not be opened due to ${err}`) | ||
} | ||
} else { | ||
return left(`error: file ${p} does not exist`) | ||
} | ||
} | ||
|
||
// read either the standard input or an input file | ||
function parseModule(argv: any): [Phase1Result, LookupTableByModule, string] { | ||
const data = readFileSync(argv.input, 'utf8') | ||
return parseText(argv, lf(data)) | ||
const res = loadFile(argv.input) | ||
if (res.isRight()) { | ||
return parseText(argv, res.value) | ||
} else { | ||
console.error(res.value) | ||
process.exit(1) | ||
} | ||
} | ||
|
||
// a callback to parse the text that we get from readFile | ||
function parseText(argv: any, text: string): [Phase1Result, LookupTableByModule, string] { | ||
const path = resolve(cwd(), argv.input) | ||
const phase1Result = parsePhase1(text, path) | ||
if (phase1Result.kind === 'error') { | ||
reportError(argv, text, phase1Result) | ||
reportParseError(argv, text, phase1Result) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an incidental change to document the nature of this function, which is not suitable for general purpose error reporting. |
||
process.exit(1) | ||
} | ||
|
||
|
@@ -125,7 +144,7 @@ function parseText(argv: any, text: string): [Phase1Result, LookupTableByModule, | |
|
||
const phase2Result = parsePhase2(phase1Result.module, phase1Result.sourceMap) | ||
if (phase2Result.kind === 'error') { | ||
reportError(argv, text, phase2Result) | ||
reportParseError(argv, text, phase2Result) | ||
process.exit(1) | ||
} | ||
|
||
|
@@ -141,7 +160,7 @@ function parseText(argv: any, text: string): [Phase1Result, LookupTableByModule, | |
return [phase1Result, phase2Result.table, text] | ||
} | ||
|
||
function reportError(argv: any, sourceCode: string, result: { kind: 'error', messages: ErrorMessage[] }) { | ||
function reportParseError(argv: any, sourceCode: string, result: { kind: 'error', messages: ErrorMessage[] }) { | ||
if (argv.out) { | ||
// write the errors to the output file | ||
writeToJson(argv.out, result) | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a nice way to handle the errors here: instead, we should chance the error-prone computations, and have error reporting at the outermost layer. But I'm introducing the change in this way just to open the conversation.
If anyone feels strongly about avoiding use of
Either
to faciliate composition of our error-apt computations here, I can revert this easily.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should totally use
Either
here. There are some other parse and name resolution functions that returnkind: 'ok'
orkind: 'error'
objects instead ofEither
because we wrote them before introducing theEither library
. We should update all of them at some point.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent. I'll swap this over while I'm refactoring to get the type map out. Thanks for the guidance and confirmation :)