ava icon indicating copy to clipboard operation
ava copied to clipboard

Exceptions from `axios` library result in Unhandled rejection

Open sflanker opened this issue 1 year ago • 3 comments

Unhandled exceptions from the axios library result in the following (even though all promises are properly awaited):

Unhandled rejection in example.test.js

  DOMException {}

Whereas I would expect something like

  axios test

  Rejected promise returned by test. Reason:

  Error {
    ...
  }

Root Cause & Suggesion

This is happening because AVA sends transmits message about events like test failure due to exceptions being raised via a channel that requires serialization, and apparently these axios exceptions are not serializable. It would be nice if the outcome were less bad in this scenario. I can understand is isn't going to be possible to provide the same fidelity in this scenario, but it would be nice if the serialization issue could be caught, and instead of sending the full error object, just send an abbreviation (like the message and stack trace, possibly with some kind of warning that serialization of the original error was not possible). As it is the outcome is extremely hard to debug.

Reproducible example (run this test, with no special config):

import test from "ava";
import axios from "axios";
async function notGood() {
    const res = await axios.post("http://nothing/bork");
    return res.status;
}
test("axios test", async (t) => {
    const val = await notGood();
    t.truthy(val);
});

Versions

    "ava": "^6.1.3",
    "axios": "^1.7.7",

sflanker avatar Sep 19 '24 05:09 sflanker

+1 for this getting fixed!

deldrid1 avatar Sep 25 '24 20:09 deldrid1

Same thing happens in [email protected] with jsdom:

import ava from "ava"
import jsdom from "jsdom"

ava("test", (test) => {
	const { JSDOM } = jsdom
	const dom = new JSDOM()
	dom.window.document.querySelector("<bar")
	test.pass()
})
Output

✘ [fail]: test Rejected promise returned by test ─

test

Rejected promise returned by test. Reason:

DOMException {}

Error at file:///home/user/playground/ava-issues-3343/node_modules/ava/lib/test.js:604:28 at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

─

1 test failed

Looks like node test runner is also swallowing the error and the output is even worse as we simply get a {}.

thoughtsunificator avatar Nov 24 '24 23:11 thoughtsunificator

I'm experiencing the same issue with JSDOM, as @thoughtsunificator demonstrated.

illarionvk avatar Nov 25 '24 11:11 illarionvk

Still happening on latest ava (6.4.1), not sure if this is happening across all reporters.

Edit: Looks like the culprit might be concordance, or at the very least how it is used or not used:

https://github.com/concordancejs/concordance/blob/791d2a89b40eb13f2c889ac270dd8be190cf8073/lib/formatUtils.js#L47-L65

As you can see, it does not seem to care for .value which holds an important part of the Error.

That's only the Error message, we also lose the stack.

// assuming [email protected]
import { JSDOM } from "jsdom"
import concordance from "concordance"

const virtualDOM = new JSDOM()
try {
	virtualDOM.window.document.querySelector("<bar")
} catch(error) {
	console.log(concordance.formatDescriptor(concordance.describe(error)))
}
Output DOMException {}

Node v24.6.0 concordance 5.0.4

thoughtsunificator avatar Aug 17 '25 22:08 thoughtsunificator