diff --git a/src/loaders/ApiBasedRepositoryLoader.ts b/src/loaders/ApiBasedRepositoryLoader.ts index a7717a4..de48179 100644 --- a/src/loaders/ApiBasedRepositoryLoader.ts +++ b/src/loaders/ApiBasedRepositoryLoader.ts @@ -156,31 +156,36 @@ export class ApiBasedRepositoryLoader { formData.set("json", JSON.stringify(formDataJsonObject)); const query = new URLSearchParams(params).toString(); - const response = await fetch(`${this.baseUrl}?${query}`, { - method: "POST", - body: formData, - }); - const json = await response.json(); - if (json.buses && json.buses["-1"] === undefined) { - const jsonBuses = Object.values(json.buses).map((busesArr: any) => { - return busesArr[0]; + try { + const response = await fetch(`${this.baseUrl}?${query}`, { + method: "POST", + body: formData, }); + const json = await response.json(); - await Promise.all(jsonBuses.map(async (jsonBus: any) => { - const constructedShuttle: IShuttle = { - name: jsonBus.bus, - coordinates: { - latitude: parseFloat(jsonBus.latitude), - longitude: parseFloat(jsonBus.longitude), - }, - routeId: jsonBus.routeId, - systemId: systemId, - id: `${jsonBus.busId}` - } + if (json.buses && json.buses["-1"] === undefined) { + const jsonBuses = Object.values(json.buses).map((busesArr: any) => { + return busesArr[0]; + }); - await this.repository.addOrUpdateShuttle(constructedShuttle); - })) + await Promise.all(jsonBuses.map(async (jsonBus: any) => { + const constructedShuttle: IShuttle = { + name: jsonBus.bus, + coordinates: { + latitude: parseFloat(jsonBus.latitude), + longitude: parseFloat(jsonBus.longitude), + }, + routeId: jsonBus.routeId, + systemId: systemId, + id: `${jsonBus.busId}` + } + + await this.repository.addOrUpdateShuttle(constructedShuttle); + })); + } + } catch(e: any) { + throw new ApiResponseError(e.message); } } @@ -207,26 +212,31 @@ export class ApiBasedRepositoryLoader { }; const query = new URLSearchParams(params).toString(); - const response = await fetch(`${this.baseUrl}?${query}`, { - method: "GET", - }); - const json = await response.json(); - if (json.ETAs && json.ETAs[stopId]) { - // Continue with the parsing - json.ETAs[stopId].forEach((jsonEta: any) => { - // Update cache - const shuttleId: string = jsonEta.busId; - - const eta: IEta = { - secondsRemaining: jsonEta.secondsSpent, - shuttleId: `${shuttleId}`, - stopId: stopId, - millisecondsSinceEpoch: Date.now(), - }; - - this.repository.addOrUpdateEta(eta); + try { + const response = await fetch(`${this.baseUrl}?${query}`, { + method: "GET", }); + const json = await response.json(); + + if (json.ETAs && json.ETAs[stopId]) { + // Continue with the parsing + json.ETAs[stopId].forEach((jsonEta: any) => { + // Update cache + const shuttleId: string = jsonEta.busId; + + const eta: IEta = { + secondsRemaining: jsonEta.secondsSpent, + shuttleId: `${shuttleId}`, + stopId: stopId, + millisecondsSinceEpoch: Date.now(), + }; + + this.repository.addOrUpdateEta(eta); + }); + } + } catch(e: any) { + throw new ApiResponseError(e.message); } }