From 076d055beef40827d7b477fe3f2f98307330dcd9 Mon Sep 17 00:00:00 2001 From: "Michael B. Gale" Date: Mon, 2 Feb 2026 18:09:44 +0000 Subject: [PATCH] Improve `sendFailedStatusReport` tests --- lib/start-proxy-action.js | 2 +- src/start-proxy.test.ts | 124 ++++++++++++++++++++++++-------------- src/start-proxy.ts | 2 +- 3 files changed, 81 insertions(+), 47 deletions(-) diff --git a/lib/start-proxy-action.js b/lib/start-proxy-action.js index 5ae3c879f..6e76b14bb 100644 --- a/lib/start-proxy-action.js +++ b/lib/start-proxy-action.js @@ -119616,7 +119616,7 @@ function getSafeErrorMessage(error3) { if (error3 instanceof StartProxyError) { return getStartProxyErrorMessage(error3.errorType); } - return `Error from start-proxy Action omitted (${typeof error3}).`; + return `Error from start-proxy Action omitted (${error3.constructor.name}).`; } async function sendFailedStatusReport(logger, startedAt, language, unwrappedError) { const error3 = wrapError(unwrappedError); diff --git a/src/start-proxy.test.ts b/src/start-proxy.test.ts index 40085408e..bd6ce0024 100644 --- a/src/start-proxy.test.ts +++ b/src/start-proxy.test.ts @@ -19,57 +19,91 @@ import { setupTests, withRecordingLoggerAsync, } from "./testing-utils"; +import { ConfigurationError } from "./util"; setupTests(test); -test("sendFailedStatusReport - does not report messages from arbitrary error types", async (t) => { - const loggedMessages = []; - const logger = getRecordingLogger(loggedMessages); - const error = new Error( +const sendFailedStatusReportTest = test.macro({ + exec: async ( + t: ExecutionContext, + err: Error, + expectedMessage: string, + expectedStatus: statusReport.ActionStatus = "failure", + ) => { + const now = new Date(); + + // Override core.setFailed to avoid it setting the program's exit code + sinon.stub(core, "setFailed").returns(); + + const createStatusReportBase = sinon.stub( + statusReport, + "createStatusReportBase", + ); + createStatusReportBase.resolves(undefined); + + await withRecordingLoggerAsync(async (logger) => { + await startProxyExports.sendFailedStatusReport( + logger, + now, + undefined, + err, + ); + + // Check that the stub has been called exactly once, with the expected arguments, + // but not with the message from the error. + sinon.assert.calledOnceWithExactly( + createStatusReportBase, + statusReport.ActionName.StartProxy, + expectedStatus, + now, + sinon.match.any, + sinon.match.any, + sinon.match.any, + expectedMessage, + ); + t.false( + createStatusReportBase.calledWith( + statusReport.ActionName.StartProxy, + expectedStatus, + now, + sinon.match.any, + sinon.match.any, + sinon.match.any, + sinon.match((msg: string) => msg.includes(err.message)), + ), + "createStatusReportBase was called with the error message", + ); + }); + }, + + title: (providedTitle = "") => `sendFailedStatusReport - ${providedTitle}`, +}); + +test( + "reports generic error message for non-StartProxyError error", + sendFailedStatusReportTest, + new Error("Something went wrong today"), + "Error from start-proxy Action omitted (Error).", +); + +test( + "reports generic error message for non-StartProxyError error with safe error message", + sendFailedStatusReportTest, + new Error( startProxyExports.getStartProxyErrorMessage( startProxyExports.StartProxyErrorType.DownloadFailed, ), - ); - const now = new Date(); + ), + "Error from start-proxy Action omitted (Error).", +); - // Override core.setFailed to avoid it setting the program's exit code - sinon.stub(core, "setFailed").returns(); - - const createStatusReportBase = sinon.stub( - statusReport, - "createStatusReportBase", - ); - createStatusReportBase.resolves(undefined); - - await startProxyExports.sendFailedStatusReport(logger, now, undefined, error); - - // Check that the stub has been called exactly once, with the expected arguments, - // but not with the message from the error. - t.assert(createStatusReportBase.calledOnce); - t.assert( - createStatusReportBase.calledWith( - statusReport.ActionName.StartProxy, - "failure", - now, - sinon.match.any, - sinon.match.any, - sinon.match.any, - ), - "createStatusReportBase wasn't called with the expected arguments", - ); - t.false( - createStatusReportBase.calledWith( - statusReport.ActionName.StartProxy, - "failure", - now, - sinon.match.any, - sinon.match.any, - sinon.match.any, - sinon.match((msg: string) => msg.includes(error.message)), - ), - "createStatusReportBase was called with the error message", - ); -}); +test( + "reports generic error message for ConfigurationError error", + sendFailedStatusReportTest, + new ConfigurationError("Something went wrong today"), + "Error from start-proxy Action omitted (ConfigurationError).", + "user-error", +); const toEncodedJSON = (data: any) => Buffer.from(JSON.stringify(data)).toString("base64"); @@ -395,7 +429,7 @@ test("getSafeErrorMessage - does not return message for arbitrary errors", (t) = t.not(message, error.message); t.assert(message.startsWith("Error from start-proxy Action omitted")); - t.assert(message.includes(typeof error)); + t.assert(message.includes(error.name)); }); const wrapFailureTest = test.macro({ diff --git a/src/start-proxy.ts b/src/start-proxy.ts index c67e5d7c8..f592d076b 100644 --- a/src/start-proxy.ts +++ b/src/start-proxy.ts @@ -115,7 +115,7 @@ export function getSafeErrorMessage(error: Error): string { } // Otherwise, omit the actual error message. - return `Error from start-proxy Action omitted (${typeof error}).`; + return `Error from start-proxy Action omitted (${error.constructor.name}).`; } /**