Made CurseForge error handling a little bit more verbose

This commit is contained in:
Kir_Antipov 2022-06-05 20:09:34 +03:00
parent 0fcfdc07c1
commit dc82753d62
2 changed files with 22 additions and 3 deletions

View file

@ -22,7 +22,7 @@ export default class ModrinthPublisher extends ModPublisher {
const projects = (await Promise.all(dependencies const projects = (await Promise.all(dependencies
.filter((x, _, self) => (x.kind !== DependencyKind.Suggests && x.kind !== DependencyKind.Includes) || !self.find(y => y.id === x.id && y.kind !== DependencyKind.Suggests && y.kind !== DependencyKind.Includes)) .filter((x, _, self) => (x.kind !== DependencyKind.Suggests && x.kind !== DependencyKind.Includes) || !self.find(y => y.id === x.id && y.kind !== DependencyKind.Suggests && y.kind !== DependencyKind.Includes))
.map(async x => ({ .map(async x => ({
project_id: (await getProject(x.getProjectSlug(this.target))).id, project_id: (await getProject(x.getProjectSlug(this.target)))?.id,
dependency_type: modrinthDependencyKinds.get(x.kind) dependency_type: modrinthDependencyKinds.get(x.kind)
})))) }))))
.filter(x => x.project_id && x.dependency_type); .filter(x => x.project_id && x.dependency_type);

View file

@ -33,6 +33,25 @@ class CurseForgeUploadError extends SoftError {
} }
} }
async function fetchJsonArray<T>(url: string): Promise<T[] | never> {
const response = await fetch(url);
if (!response.ok) {
const isServerError = response.status >= 500;
throw new SoftError(isServerError, `${response.status} (${response.statusText})`);
}
let array: T[];
try {
array = await response.json();
} catch {
array = null;
}
if (!Array.isArray(array)) {
throw new SoftError(true, "CurseForge sometimes returns Cloudflare's HTML page instead of its API response. Yeah, I know, very cool. Just wait 15-20 minutes, then try re-running this action, and you should be fine.");
}
return array;
}
let cachedCurseForgeVersions: CurseForgeVersions = null; let cachedCurseForgeVersions: CurseForgeVersions = null;
async function getCurseForgeVersions(token: string): Promise<CurseForgeVersions> { async function getCurseForgeVersions(token: string): Promise<CurseForgeVersions> {
@ -43,12 +62,12 @@ async function getCurseForgeVersions(token: string): Promise<CurseForgeVersions>
} }
async function loadCurseForgeVersions(token: string): Promise<CurseForgeVersions> { async function loadCurseForgeVersions(token: string): Promise<CurseForgeVersions> {
const versionTypes = <{ id: number, slug: string }[]>await (await fetch(`${baseUrl}/game/version-types?token=${token}`)).json(); const versionTypes = await fetchJsonArray<{ id: number, slug: string }>(`${baseUrl}/game/version-types?token=${token}`);
const javaVersionTypes = versionTypes.filter(x => x.slug.startsWith("java")).map(x => x.id); const javaVersionTypes = versionTypes.filter(x => x.slug.startsWith("java")).map(x => x.id);
const minecraftVersionTypes = versionTypes.filter(x => x.slug.startsWith("minecraft")).map(x => x.id); const minecraftVersionTypes = versionTypes.filter(x => x.slug.startsWith("minecraft")).map(x => x.id);
const loaderVersionTypes = versionTypes.filter(x => x.slug.startsWith("modloader")).map(x => x.id); const loaderVersionTypes = versionTypes.filter(x => x.slug.startsWith("modloader")).map(x => x.id);
const versions = <CurseForgeVersion[]>await (await fetch(`${baseUrl}/game/versions?token=${token}`)).json(); const versions = await fetchJsonArray<CurseForgeVersion>(`${baseUrl}/game/versions?token=${token}`);
return versions.reduce((container, version) => { return versions.reduce((container, version) => {
if (javaVersionTypes.includes(version.gameVersionTypeID)) { if (javaVersionTypes.includes(version.gameVersionTypeID)) {
container.java.push(version); container.java.push(version);