Skip to content

Commit 42fb267

Browse files
committed
Don't store error message in StartProxyError errors
1 parent 832a783 commit 42fb267

File tree

3 files changed

+60
-18
lines changed

3 files changed

+60
-18
lines changed

lib/start-proxy-action.js

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/start-proxy.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ import {
2222

2323
setupTests(test);
2424

25-
test("sendFailedStatusReport - does not report arbitrary error messages", async (t) => {
25+
test("sendFailedStatusReport - does not report messages from arbitrary error types", async (t) => {
2626
const loggedMessages = [];
2727
const logger = getRecordingLogger(loggedMessages);
28-
const error = new Error(startProxyExports.StartProxyErrorType.DownloadFailed);
28+
const error = new Error(
29+
startProxyExports.getStartProxyErrorMessage(
30+
startProxyExports.StartProxyErrorType.DownloadFailed,
31+
),
32+
);
2933
const now = new Date();
3034

3135
// Override core.setFailed to avoid it setting the program's exit code
@@ -374,11 +378,18 @@ test("getSafeErrorMessage - returns actual message for `StartProxyError`", (t) =
374378
const error = new startProxyExports.StartProxyError(
375379
startProxyExports.StartProxyErrorType.DownloadFailed,
376380
);
377-
t.is(startProxyExports.getSafeErrorMessage(error), error.message);
381+
t.is(
382+
startProxyExports.getSafeErrorMessage(error),
383+
startProxyExports.getStartProxyErrorMessage(error.errorType),
384+
);
378385
});
379386

380387
test("getSafeErrorMessage - does not return message for arbitrary errors", (t) => {
381-
const error = new Error(startProxyExports.StartProxyErrorType.DownloadFailed);
388+
const error = new Error(
389+
startProxyExports.getStartProxyErrorMessage(
390+
startProxyExports.StartProxyErrorType.DownloadFailed,
391+
),
392+
);
382393

383394
const message = startProxyExports.getSafeErrorMessage(error);
384395

src/start-proxy.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,29 @@ import * as util from "./util";
2424
import { ConfigurationError, getErrorMessage, isDefined } from "./util";
2525

2626
/**
27-
* Enumerates specific error types, along with suitable error messages, for errors
28-
* that we want to track in status reports.
27+
* Enumerates specific error types for which we have corresponding error messages that
28+
* are safe to include in status reports.
2929
*/
3030
export enum StartProxyErrorType {
31-
DownloadFailed = "Failed to download proxy archive.",
32-
ExtractionFailed = "Failed to extract proxy archive.",
33-
CacheFailed = "Failed to add proxy to toolcache",
31+
DownloadFailed,
32+
ExtractionFailed,
33+
CacheFailed,
34+
}
35+
36+
/**
37+
* @returns The error message corresponding to the error type.
38+
*/
39+
export function getStartProxyErrorMessage(
40+
errorType: StartProxyErrorType,
41+
): string {
42+
switch (errorType) {
43+
case StartProxyErrorType.DownloadFailed:
44+
return "Failed to download proxy archive.";
45+
case StartProxyErrorType.ExtractionFailed:
46+
return "Failed to extract proxy archive.";
47+
case StartProxyErrorType.CacheFailed:
48+
return "Failed to add proxy to toolcache";
49+
}
3450
}
3551

3652
/**
@@ -40,8 +56,11 @@ export enum StartProxyErrorType {
4056
* `StartProxyErrorType` and therefore safe to include in a status report.
4157
*/
4258
export class StartProxyError extends Error {
59+
public readonly errorType: StartProxyErrorType;
60+
4361
constructor(errorType: StartProxyErrorType) {
44-
super(errorType);
62+
super();
63+
this.errorType = errorType;
4564
}
4665
}
4766

@@ -89,10 +108,10 @@ export async function sendSuccessStatusReport(
89108
* @param error The error for which to get an error message.
90109
*/
91110
export function getSafeErrorMessage(error: Error): string {
92-
// If the error is a `StartProxyError`, the constructor ensures that the
93-
// message comes from `StartProxyErrorType`.
111+
// If the error is a `StartProxyError`, resolve the error type to the corresponding
112+
// error message.
94113
if (error instanceof StartProxyError) {
95-
return error.message;
114+
return getStartProxyErrorMessage(error.errorType);
96115
}
97116

98117
// Otherwise, omit the actual error message.

0 commit comments

Comments
 (0)