Add try-catch to avoid failing requests

On GHES servers below version 3.8, the variables context is unavailable, resulting in 404 errors when calling the corresponding endpoint.
This commit is contained in:
flmeyer
2023-05-12 22:42:09 +02:00
parent 57a77551b0
commit 468b68840b
@@ -2,9 +2,10 @@ import {data, DescriptionDictionary} from "@actions/expressions";
import {Pair} from "@actions/expressions/data/expressiondata";
import {StringData} from "@actions/expressions/data/index";
import {WorkflowContext} from "@actions/languageservice/context/workflow-context";
import {warn} from "@actions/languageservice/log";
import {log, warn} from "@actions/languageservice/log";
import {isMapping, isString} from "@actions/workflow-parser";
import {Octokit} from "@octokit/rest";
import {RequestError} from "@octokit/types";
import {RepositoryContext} from "../initializationOptions";
import {TTLCache} from "../utils/cache";
@@ -42,6 +43,7 @@ export async function getVariables(
}
const variablesContext = defaultContext || new DescriptionDictionary();
try {
const variables = await getRemoteVariables(octokit, cache, repo, environmentName);
// Build combined map of variables
@@ -86,6 +88,13 @@ export async function getVariables(
.forEach(variable => variablesContext?.add(variable.key, variable.value, variable.description));
return variablesContext;
} catch (e: any) {
const requestError: RequestError = e;
if (requestError.name == "HttpError" && requestError.status == 404) {
log("Failure to request variables. Ignore if you're using GitHub Enterprise Server below version 3.8");
return variablesContext;
} else throw e;
}
}
export async function getRemoteVariables(