add error pathway and test case for validation

This commit is contained in:
2025-01-22 14:01:57 -08:00
parent 6c9f4691fd
commit 1b9a77209f
6 changed files with 488 additions and 19 deletions

View File

@@ -1,6 +1,13 @@
import { GetterSetterRepository } from "../repositories/GetterSetterRepository";
import { IEta, IRoute, IShuttle, IStop, ISystem } from "../entities/entities";
export class ApiResponseError extends Error {
constructor(message: string) {
super(message);
this.name = "ApiResponseError";
}
}
export class ApiBasedRepositoryLoader {
readonly supportedSystemIds = ["263"];
readonly baseUrl = "https://passiogo.com/mapGetData.php";
@@ -15,20 +22,32 @@ export class ApiBasedRepositoryLoader {
getSystems: "2",
};
const query = new URLSearchParams(params).toString();
const response = await fetch(`${this.baseUrl}?${query}`);
const json = await response.json()
if (typeof json.all === "object") {
// filter down to supported systems
const filteredSystems = json.all.filter((jsonSystem: any) => this.supportedSystemIds.includes(jsonSystem.id));
await Promise.all(filteredSystems.map(async (system: any) => {
const constructedSystem: ISystem = {
id: system.id,
name: system.fullname,
};
try {
const response = await fetch(`${this.baseUrl}?${query}`);
const json = await response.json();
await this.repository.addOrUpdateSystem(constructedSystem);
}));
if (!response.ok) {
throw new Error(`HTTP error with status ${response.status}`)
}
if (typeof json.all === "object") {
// filter down to supported systems
const filteredSystems = json.all.filter((jsonSystem: any) => this.supportedSystemIds.includes(jsonSystem.id));
await Promise.all(filteredSystems.map(async (system: any) => {
const constructedSystem: ISystem = {
id: system.id,
name: system.fullname,
};
await this.repository.addOrUpdateSystem(constructedSystem);
}));
} else {
throw new Error("Received JSON object does not contain `all` field")
}
} catch(e: any) {
console.error("fetchAndUpdateSystemData call failed: ", e);
throw new ApiResponseError(e.message);
}
}