Improve stack information when logging network error.
If fetch call returns an error, the thrown error will only have a stack
trace starting from Scheduler, which is not useful. By re-throwing and
stitching stacks together we get better information. We have to pass the
new error into fireNetworkError and errFn, to make sure the same benefit
is observed in the event logs and for plugins.
This is not exhaustive. There are still some examples of the erors which
get thrown without extra stack information, which I couldn't figure out
how are they getting called.
This also exemplifies likely unintended behaviour when rest api is
called in gr-plugin-rest-api. Since errFn is provided the network errors
are not being reported as fireNetworkError and end up being reported as
"unhandledrejection" instead.
Release-Notes: skip
Change-Id: I650dcaaf35d0b49ff8bae4173cc5a3e78ac1d57a
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts
index 2da1fb8..310c360 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper.ts
@@ -355,12 +355,17 @@
try {
resp = await this.fetchImpl(fetchReq);
} catch (err) {
+ // Wrap the error to get more information about the stack.
+ const newErr = new Error(
+ `Network error when trying to fetch. Cause: ${(err as Error).message}`
+ );
+ newErr.stack = (newErr.stack ?? '') + '\n' + ((err as Error).stack ?? '');
if (req.errFn) {
- await req.errFn.call(undefined, null, err as Error);
+ await req.errFn.call(undefined, null, newErr);
} else {
- fireNetworkError(err as Error);
+ fireNetworkError(newErr);
}
- throw err;
+ throw newErr;
}
if (req.reportServerError && !resp.ok) {
if (req.errFn) {
diff --git a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper_test.ts b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper_test.ts
index 0f94a4b..0f7acae 100644
--- a/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper_test.ts
+++ b/polygerrit-ui/app/elements/shared/gr-rest-api-interface/gr-rest-apis/gr-rest-api-helper_test.ts
@@ -206,7 +206,10 @@
const promise = helper.fetchJSON({url: '/dummy/url'});
await assertReadRequest();
const err = await assertFails(promise);
- assert.equal((err as Error).message, 'No response');
+ assert.equal(
+ (err as Error).message,
+ 'Network error when trying to fetch. Cause: No response'
+ );
await waitEventLoop();
assert.isTrue(networkErrorCalled);
assert.isFalse(serverErrorCalled);
@@ -221,7 +224,10 @@
});
await assertReadRequest();
const err = await assertFails(promise);
- assert.equal((err as Error).message, 'No response');
+ assert.equal(
+ (err as Error).message,
+ 'Network error when trying to fetch. Cause: No response'
+ );
await waitEventLoop();
assert.isTrue(errFn.called);
assert.isFalse(networkErrorCalled);