Skip to content

Commit

Permalink
Improved error catching and logging (#343)
Browse files Browse the repository at this point in the history
* nicer message when unable to call v4

* test verifies proper message for v4 403 query result

* test verifies proper message for v4 403 query result

* v4 access message shows up on 410s instead of 403s

* remove debug code

* fix error reporting when v10 queries are run against v4

* handle unknown errors more gracefully

* run linter

* fix test

* don't need to pass version

---------

Co-authored-by: Ashton Eby <[email protected]>
  • Loading branch information
mwilde345 and echo-bravo-yahoo authored Jul 8, 2024
1 parent 65b2833 commit c8ade45
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
1 change: 1 addition & 0 deletions coverage/tmp/coverage-70892-1720444763573-0.json

Large diffs are not rendered by default.

26 changes: 15 additions & 11 deletions src/commands/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,17 +202,21 @@ class EvalCommand extends FaunaCommand {
const response = await runQueries(res.body, client);
return await writeFormattedOutput(outputFile, response, flags.format);
} catch (error) {
this.error(
error.faunaError instanceof faunadb.errors.FaunaHTTPError
? util.inspect(
JSON.parse(error.faunaError.requestResult.responseRaw),
{
depth: null,
compact: false,
}
)
: error.faunaError.message
);
if (error.faunaError === undefined) {
// this happens when wrapQueries fails during the runInContext step
// at that point, we have Errors that didn't get run as a query, so
// they don't have a .faunaError property
this.error(error.message);
} else if (error.faunaError instanceof faunadb.errors.FaunaHTTPError) {
this.error(
util.inspect(JSON.parse(error.faunaError.requestResult.responseRaw), {
depth: null,
compact: false,
})
);
} else {
this.error(error.faunaError.message);
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib/fauna-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,11 @@ class FaunaCommand extends Command {
this.error(
`Could not Connect to ${connectionOptions.url} Unauthorized Secret`
);
} else {
const code = err?.message ? `${err.message}: ` : "";
const details = err?.description ?? "";
this.error(`${code}${details}`);
}
this.error(err);
}

async getClient({ dbScope, role, version } = {}) {
Expand Down
20 changes: 13 additions & 7 deletions src/lib/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,21 @@ class QueryError extends Error {
function wrapQueries(expressions, client) {
const q = query;
createContext(q);
return expressions.map(
(exp, queryNumber) => () =>
client.query(runInContext(generate(exp), q)).catch((err) => {
throw new QueryError(generate(exp), err, queryNumber + 1);
})
);
return expressions.map((exp, queryNumber) => () => {
let query;
try {
query = runInContext(generate(exp), q);
} catch (e) {
return Promise.reject(e);
}

return client.query(query).catch((err) => {
throw new QueryError(generate(exp), err, queryNumber + 1);
});
});
}

export function runQueries(expressions, client) {
export async function runQueries(expressions, client) {
if (expressions.length === 1) {
var f = wrapQueries(expressions, client)[0];
return f();
Expand Down
13 changes: 13 additions & 0 deletions test/commands/eval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ describe("eval", () => {
expect(e.message).to.contain("transaction aborted");
})
.it("It pretty-prints an error message the command fails");

test
.nock(getEndpoint(), { allowUnmocked: true }, (api) => {
api.post("/", matchFqlReq(q.Now())).reply(410, {
errors: [{ description: "v4 error message from core" }],
});
})
.stderr()
.command(withOpts(["eval", "--version", "4", "1"]))
.catch((e) => {
expect(e.message).to.contain("v4 error message from core");
})
.it("410 from core displays core error message");
});

describe("eval in v10", () => {
Expand Down

0 comments on commit c8ade45

Please sign in to comment.