Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert "Invalid source root" errors to UserErrors #1282

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
46 changes: 31 additions & 15 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.

55 changes: 37 additions & 18 deletions src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,7 @@ export async function runInit(
}
}
} catch (e) {
// Handle the situation where init is called twice
// for the same database in the same job.
if (
e instanceof Error &&
e.message?.includes("Refusing to create databases") &&
e.message.includes("exists and is not an empty directory.")
) {
throw new util.UserError(
`Is the "init" action called twice in the same job? ${e.message}`
);
} else if (
e instanceof Error &&
e.message?.includes("is not compatible with this CodeQL CLI")
) {
throw new util.UserError(e.message);
} else {
throw e;
}
throw processError(e);
}
return await getCombinedTracerConfig(
config,
Expand All @@ -144,6 +127,42 @@ export async function runInit(
);
}

/**
* Possibly convert this error into a UserError in order to avoid
* counting this error towards our internal error budget.
*
* @param e The error to possibly convert to a UserError.
*
* @returns A UserError if the error is a known error that can be
* attributed to the user, otherwise the original error.
*/
function processError(e: any): Error {
if (!(e instanceof Error)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious: when would e not be an instanceof Error if it was sent from the try/catch block? Or is this just good practice in any error handling function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In javascript, you can "throw" anything. So you are not guaranteed that a catch clause is actually catching an Error instance. In practice, you should only throw errors. Typescript is just being pedantic here, and so is this code.

return e;
}

if (
// Init action called twice
e.message?.includes("Refusing to create databases") &&
e.message?.includes("exists and is not an empty directory.")
) {
return new util.UserError(
`Is the "init" action called twice in the same job? ${e.message}`
);
}

if (
// Version of CodeQL CLI is incompatible with this version of the CodeQL Action
e.message?.includes("is not compatible with this CodeQL CLI") ||
// Expected source location for database creation does not exist
e.message?.includes("Invalid source root")
) {
return new util.UserError(e.message);
}

return e;
}

// Runs a powershell script to inject the tracer into a parent process
// so it can tracer future processes, hopefully including the build process.
// If processName is given then injects into the nearest parent process with
Expand Down