Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add processCliConfigurationError method
  • Loading branch information
angelapwen committed Feb 6, 2024
commit 7fa31b735c1fd49698c430cb2bf97e9049e7e0ce
34 changes: 33 additions & 1 deletion lib/cli-config-errors.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/cli-config-errors.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions lib/codeql.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/codeql.js.map

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions lib/init.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/init.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions src/cli-config-errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** Error messages from the CLI that we handle specially. */
export enum CliError {
IncompatibleWithActionVersion,
InitCalledTwice,
Expand Down Expand Up @@ -74,3 +75,47 @@ export function isCliConfigurationError(
}
return true;
}

/**
* Maps a CLI error class to the error message that the Action should return in
* case of this error. Leave undefined if the CLI error message should be returned
* directly.
*
* Otherwise, specify an error message to return for this CLI error; and whether the
* original CLI error text should be appended to it.
*/
export const cliToActionErrorsConfig: Record<
CliError,
| {
actionErrorMessage: string;
appendCliError: boolean;
}
| undefined
> = {
[CliError.InitCalledTwice]: {
actionErrorMessage: `Is the "init" action called twice in the same job?`,
appendCliError: true,
},
[CliError.NoJavaScriptTypeScriptCodeFound]: {
actionErrorMessage:
"No code found during the build. Please see: " +
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build",
appendCliError: false,
},
[CliError.IncompatibleWithActionVersion]: undefined,
[CliError.InvalidSourceRoot]: undefined,
};

export function processCliConfigurationError(
cliError: CliError,
cliErrorMessage: string,
): string {
const cliToActionErrorConfig = cliToActionErrorsConfig[cliError];
if (cliToActionErrorConfig === undefined) {
return cliErrorMessage;
}

return cliToActionErrorConfig.appendCliError
? cliToActionErrorConfig.actionErrorMessage + cliErrorMessage
: cliToActionErrorConfig.actionErrorMessage;
}
32 changes: 25 additions & 7 deletions src/codeql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
isAnalyzingDefaultBranch,
} from "./actions-util";
import * as api from "./api-client";
import { CliError, isCliConfigurationError } from "./cli-config-errors";
import {
CliError,
isCliConfigurationError,
processCliConfigurationError,
} from "./cli-config-errors";
import type { Config } from "./config-utils";
import { EnvVar } from "./environment";
import {
Expand Down Expand Up @@ -650,17 +654,29 @@ export async function getCodeQLForCmd(
if (e instanceof CommandInvocationError) {
if (isCliConfigurationError(CliError.InitCalledTwice, e.message)) {
throw new util.UserError(
`Is the "init" action called twice in the same job? ${e.message}`,
processCliConfigurationError(CliError.InitCalledTwice, e.message),
);
}
if (
isCliConfigurationError(
CliError.IncompatibleWithActionVersion,
e.message,
) ||
isCliConfigurationError(CliError.InvalidSourceRoot, e.message)
)
) {
throw new util.UserError(e.message);
throw new util.UserError(
processCliConfigurationError(
CliError.IncompatibleWithActionVersion,
e.message,
),
);
}
if (isCliConfigurationError(CliError.InvalidSourceRoot, e.message)) {
throw new util.UserError(
processCliConfigurationError(
CliError.InvalidSourceRoot,
e.message,
),
);
}
}
throw e;
Expand Down Expand Up @@ -741,8 +757,10 @@ export async function getCodeQLForCmd(
)
) {
throw new util.UserError(
"No code found during the build. Please see: " +
"https://gh.io/troubleshooting-code-scanning/no-source-code-seen-during-build",
processCliConfigurationError(
CliError.NoJavaScriptTypeScriptCodeFound,
e.stderr,
),
);
}
throw e;
Expand Down