mirror of
https://github.com/louislam/uptime-kuma.git
synced 2026-03-03 02:57:01 +00:00
fix: Expand the logging around AggregateError (#6664)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -108,16 +108,51 @@ class NotificationProvider {
|
|||||||
* @throws {any} The error specified
|
* @throws {any} The error specified
|
||||||
*/
|
*/
|
||||||
throwGeneralAxiosError(error) {
|
throwGeneralAxiosError(error) {
|
||||||
let msg = "Error: " + error + " ";
|
let msg = error && error.message ? error.message : String(error);
|
||||||
|
|
||||||
if (error.response && error.response.data) {
|
if (error && error.code) {
|
||||||
|
msg += ` (code=${error.code})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error && error.response && error.response.status) {
|
||||||
|
msg += ` (HTTP ${error.response.status}${error.response.statusText ? " " + error.response.statusText : ""})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error && error.response && error.response.data) {
|
||||||
if (typeof error.response.data === "string") {
|
if (typeof error.response.data === "string") {
|
||||||
msg += error.response.data;
|
msg += " " + error.response.data;
|
||||||
} else {
|
} else {
|
||||||
msg += JSON.stringify(error.response.data);
|
try {
|
||||||
|
msg += " " + JSON.stringify(error.response.data);
|
||||||
|
} catch (e) {
|
||||||
|
msg += " " + String(error.response.data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Expand AggregateError to show underlying causes
|
||||||
|
let agg = null;
|
||||||
|
if (error && error.name === "AggregateError" && Array.isArray(error.errors)) {
|
||||||
|
agg = error;
|
||||||
|
} else if (error && error.cause && error.cause.name === "AggregateError" && Array.isArray(error.cause.errors)) {
|
||||||
|
agg = error.cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (agg) {
|
||||||
|
let causes = agg.errors
|
||||||
|
.map((e) => {
|
||||||
|
let m = e && e.message ? e.message : String(e);
|
||||||
|
if (e && e.code) {
|
||||||
|
m += ` (code=${e.code})`;
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
})
|
||||||
|
.join("; ");
|
||||||
|
msg += " - caused by: " + causes;
|
||||||
|
} else if (error && error.cause && error.cause.message) {
|
||||||
|
msg += " - cause: " + error.cause.message;
|
||||||
|
}
|
||||||
|
|
||||||
throw new Error(msg);
|
throw new Error(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
const { describe, test } = require("node:test");
|
||||||
|
const assert = require("node:assert");
|
||||||
|
|
||||||
|
const NotificationProvider = require("../../../server/notification-providers/notification-provider");
|
||||||
|
|
||||||
|
describe("NotificationProvider.throwGeneralAxiosError()", () => {
|
||||||
|
const provider = new NotificationProvider();
|
||||||
|
|
||||||
|
test("expands AggregateError causes", () => {
|
||||||
|
let err1 = new Error("connect ECONNREFUSED 127.0.0.1:443");
|
||||||
|
err1.code = "ECONNREFUSED";
|
||||||
|
let err2 = new Error("connect ECONNREFUSED ::1:443");
|
||||||
|
err2.code = "ECONNREFUSED";
|
||||||
|
|
||||||
|
let aggErr = new AggregateError([err1, err2], "AggregateError");
|
||||||
|
|
||||||
|
assert.throws(() => provider.throwGeneralAxiosError(aggErr), {
|
||||||
|
message: /^AggregateError - caused by: .+/,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("expands AggregateError wrapped in error.cause", () => {
|
||||||
|
let innerErr = new Error("connect ETIMEDOUT 10.0.0.1:443");
|
||||||
|
innerErr.code = "ETIMEDOUT";
|
||||||
|
|
||||||
|
let aggErr = new AggregateError([innerErr], "AggregateError");
|
||||||
|
let outerErr = new Error("Request failed");
|
||||||
|
outerErr.cause = aggErr;
|
||||||
|
|
||||||
|
assert.throws(() => provider.throwGeneralAxiosError(outerErr), {
|
||||||
|
message: /^Request failed - caused by: .+/,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user