Skip to content

Commit 253c29f

Browse files
committed
Add processCliConfigurationError method
1 parent 5518628 commit 253c29f

6 files changed

Lines changed: 112 additions & 16 deletions

File tree

lib/cli-config-errors.js

Lines changed: 33 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/cli-config-errors.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/codeql.js

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

lib/codeql.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli-config-errors.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/** Error messages from the CLI that we handle specially. */
12
export enum CliError {
23
IncompatibleWithActionVersion,
34
InitCalledTwice,
@@ -74,3 +75,47 @@ export function isCliConfigurationError(
7475
}
7576
return true;
7677
}
78+
79+
/**
80+
* Maps a CLI error class to the error message that the Action should return in
81+
* case of this error. Leave undefined if the CLI error message should be returned
82+
* directly.
83+
*
84+
* Otherwise, specify an error message to return for this CLI error; and whether the
85+
* original CLI error text should be appended to it.
86+
*/
87+
export const cliToActionErrorsConfig: Record<
88+
CliError,
89+
| {
90+
actionErrorMessage: string;
91+
appendCliError: boolean;
92+
}
93+
| undefined
94+
> = {
95+
[CliError.InitCalledTwice]: {
96+
actionErrorMessage: `Is the "init" action called twice in the same job?`,
97+
appendCliError: true,
98+
},
99+
[CliError.NoJavaScriptTypeScriptCodeFound]: {
100+
actionErrorMessage:
101+
"No code found during the build. Please see: " +
102+
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build",
103+
appendCliError: false,
104+
},
105+
[CliError.IncompatibleWithActionVersion]: undefined,
106+
[CliError.InvalidSourceRoot]: undefined,
107+
};
108+
109+
export function processCliConfigurationError(
110+
cliError: CliError,
111+
cliErrorMessage: string,
112+
): string {
113+
const cliToActionErrorConfig = cliToActionErrorsConfig[cliError];
114+
if (cliToActionErrorConfig === undefined) {
115+
return cliErrorMessage;
116+
}
117+
118+
return cliToActionErrorConfig.appendCliError
119+
? cliToActionErrorConfig.actionErrorMessage + cliErrorMessage
120+
: cliToActionErrorConfig.actionErrorMessage;
121+
}

src/codeql.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212
isAnalyzingDefaultBranch,
1313
} from "./actions-util";
1414
import * as api from "./api-client";
15-
import { CliError, isCliConfigurationError } from "./cli-config-errors";
15+
import {
16+
CliError,
17+
isCliConfigurationError,
18+
processCliConfigurationError,
19+
} from "./cli-config-errors";
1620
import type { Config } from "./config-utils";
1721
import { EnvVar } from "./environment";
1822
import {
@@ -636,17 +640,29 @@ export async function getCodeQLForCmd(
636640
if (e instanceof CommandInvocationError) {
637641
if (isCliConfigurationError(CliError.InitCalledTwice, e.message)) {
638642
throw new util.UserError(
639-
`Is the "init" action called twice in the same job? ${e.message}`,
643+
processCliConfigurationError(CliError.InitCalledTwice, e.message),
640644
);
641645
}
642646
if (
643647
isCliConfigurationError(
644648
CliError.IncompatibleWithActionVersion,
645649
e.message,
646-
) ||
647-
isCliConfigurationError(CliError.InvalidSourceRoot, e.message)
650+
)
648651
) {
649-
throw new util.UserError(e.message);
652+
throw new util.UserError(
653+
processCliConfigurationError(
654+
CliError.IncompatibleWithActionVersion,
655+
e.message,
656+
),
657+
);
658+
}
659+
if (isCliConfigurationError(CliError.InvalidSourceRoot, e.message)) {
660+
throw new util.UserError(
661+
processCliConfigurationError(
662+
CliError.InvalidSourceRoot,
663+
e.message,
664+
),
665+
);
650666
}
651667
}
652668
throw e;
@@ -727,8 +743,10 @@ export async function getCodeQLForCmd(
727743
)
728744
) {
729745
throw new util.UserError(
730-
"No code found during the build. Please see: " +
731-
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build",
746+
processCliConfigurationError(
747+
CliError.NoJavaScriptTypeScriptCodeFound,
748+
e.stderr,
749+
),
732750
);
733751
}
734752
throw e;

0 commit comments

Comments
 (0)