Files
vscode-codeql/extensions/ql-vscode/src/variant-analysis/custom-errors.ts
Koen Vlaswinkel 82c2952059 Add custom error handler for missing default branch
When the GitHub API returns an error for a missing default branch, we
will now show a custom error message. This custom error message includes
a link to the page to create the branch. The error is detected using the
`errors` field on the response that is now being returned.
2023-10-17 14:57:19 +02:00

86 lines
2.0 KiB
TypeScript

import { RequestError } from "@octokit/request-error";
import { NotificationLogger, showAndLogErrorMessage } from "../common/logging";
type ApiError = {
resource: string;
field: string;
code: string;
};
type ErrorResponse = {
message: string;
errors?: ApiError[];
};
export function handleRequestError(
e: RequestError,
logger: NotificationLogger,
): boolean {
if (e.status !== 422) {
return false;
}
if (!e.response?.data) {
return false;
}
const data = e.response.data;
if (!isErrorResponse(data)) {
return false;
}
if (!data.errors) {
return false;
}
// This is the only custom error message we have
const missingDefaultBranchError = data.errors.find(
(error) =>
error.resource === "Repository" &&
error.field === "default_branch" &&
error.code === "missing",
);
if (!missingDefaultBranchError) {
return false;
}
if (
!("repository" in missingDefaultBranchError) ||
typeof missingDefaultBranchError.repository !== "string"
) {
return false;
}
if (
!("default_branch" in missingDefaultBranchError) ||
typeof missingDefaultBranchError.default_branch !== "string"
) {
return false;
}
const createBranchURL = `https://github.com/${
missingDefaultBranchError.repository
}/new/${encodeURIComponent(missingDefaultBranchError.default_branch)}`;
void showAndLogErrorMessage(
logger,
`Variant analysis failed because the controller repository ${missingDefaultBranchError.repository} does not have a branch '${missingDefaultBranchError.default_branch}'. ` +
`Please create a '${missingDefaultBranchError.default_branch}' branch by clicking [here](${createBranchURL}) and re-run the variant analysis query.`,
{
fullMessage: e.message,
},
);
return true;
}
function isErrorResponse(obj: unknown): obj is ErrorResponse {
return (
typeof obj === "object" &&
obj !== null &&
"message" in obj &&
typeof obj.message === "string"
);
}