Turn enablement errors into configuration errors

This commit is contained in:
Michael B. Gale
2025-10-28 21:17:30 +00:00
parent ac9aeee226
commit 53acf0b8aa
8 changed files with 120 additions and 0 deletions

View File

@@ -169,4 +169,39 @@ test("wrapApiConfigurationError correctly wraps specific configuration errors",
res,
new util.ConfigurationError("Resource not accessible by integration"),
);
// Enablement errors.
const codeSecurityNotEnabledError = new util.HTTPError(
"Code Security must be enabled for this repository to use code scanning",
403,
);
res = api.wrapApiConfigurationError(codeSecurityNotEnabledError);
t.deepEqual(
res,
new util.ConfigurationError(
`Please verify that the necessary features are enabled: ${codeSecurityNotEnabledError.message}`,
),
);
const advancedSecurityNotEnabledError = new util.HTTPError(
"Advanced Security must be enabled for this repository to use code scanning",
403,
);
res = api.wrapApiConfigurationError(advancedSecurityNotEnabledError);
t.deepEqual(
res,
new util.ConfigurationError(
`Please verify that the necessary features are enabled: ${advancedSecurityNotEnabledError.message}`,
),
);
const codeScanningNotEnabledError = new util.HTTPError(
"Code Scanning is not enabled for this repository. Please enable code scanning in the repository settings.",
403,
);
res = api.wrapApiConfigurationError(codeScanningNotEnabledError);
t.deepEqual(
res,
new util.ConfigurationError(
`Please verify that the necessary features are enabled: ${codeScanningNotEnabledError.message}`,
),
);
});

View File

@@ -283,6 +283,14 @@ export async function getRepositoryProperties(repositoryNwo: RepositoryNwo) {
});
}
function isEnablementError(msg: string) {
return [
/Code Security must be enabled/,
/Advanced Security must be enabled/,
/Code Scanning is not enabled/,
].some((pattern) => pattern.test(msg));
}
export function wrapApiConfigurationError(e: unknown) {
const httpError = asHTTPError(e);
if (httpError !== undefined) {
@@ -304,6 +312,11 @@ export function wrapApiConfigurationError(e: unknown) {
"Please check that your token is valid and has the required permissions: contents: read, security-events: write",
);
}
if (isEnablementError(httpError.message)) {
return new ConfigurationError(
`Please verify that the necessary features are enabled: ${httpError.message}`,
);
}
if (httpError.status === 429) {
return new ConfigurationError("API rate limit exceeded");
}