Skip to content

Commit

Permalink
Catch exit() errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaines committed Jun 15, 2023
1 parent 28f88ef commit 4fb4276
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Catch the error Emscripten throws when exit() is called.

* Don't add an error message about no valid graphs in input.

* Improve error message handling when an error reported from agerr has multiple lines.
Expand Down
20 changes: 16 additions & 4 deletions src/viz.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ function parseAgerrMessages(messages) {
return result;
}

function parseErrorMessages(module) {
return parseAgerrMessages(module["agerrMessages"]).concat(parseStderrMessages(module["stderrMessages"]));
}

function render(module, src, options) {
let srcPointer, resultPointer;

Expand All @@ -53,21 +57,29 @@ function render(module, src, options) {

resultPointer = module.ccall("viz_render_string", "number", ["number", "string", "string"], [srcPointer, options.format, options.engine]);

const errors = parseAgerrMessages(module["agerrMessages"]).concat(parseStderrMessages(module["stderrMessages"]));

if (resultPointer === 0) {
return {
status: "failure",
output: undefined,
errors
errors: parseErrorMessages(module)
};
}

return {
status: "success",
output: module.UTF8ToString(resultPointer),
errors
errors: parseErrorMessages(module)
};
} catch (error) {
if (/^exit\(\d+\)/.test(error)) {
return {
status: "failure",
output: undefined,
errors: parseErrorMessages(module)
};
} else {
throw error;
}
} finally {
if (srcPointer) {
module.ccall("free", "number", ["number"], [srcPointer]);
Expand Down
13 changes: 13 additions & 0 deletions test/standalone.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ describe("standalone", function() {
]
});
});

it("returns an error if exit() is called", function() {
const result = viz.render("graph { a[label=<>] }");

assert.deepStrictEqual(result,{
status: "failure",
output: undefined,
errors: [
{ level: "error", message: "syntax error in line 1" },
{ level: "error", message: "... <HTML></HTML> ..." }
]
});
});
});

describe("graphvizVersion", function() {
Expand Down

0 comments on commit 4fb4276

Please sign in to comment.