Improve error handling

This commit is contained in:
Gabriel Augendre 2021-11-14 12:46:35 +01:00
parent 97d2d4c460
commit 8f84ffdee5

View file

@ -97,15 +97,32 @@ class App extends React.Component<{}, IAppState> {
const velovInfoPromise = http<IStationsInfoWrapper>("https://transport.data.gouv.fr/gbfs/lyon/station_information.json", {method: "GET"}); const velovInfoPromise = http<IStationsInfoWrapper>("https://transport.data.gouv.fr/gbfs/lyon/station_information.json", {method: "GET"});
const velovStatusPromise = http<IStationsStatusWrapper>("https://transport.data.gouv.fr/gbfs/lyon/station_status.json", {method: "GET"}); const velovStatusPromise = http<IStationsStatusWrapper>("https://transport.data.gouv.fr/gbfs/lyon/station_status.json", {method: "GET"});
Promise.all([tclPromise, velovInfoPromise, velovStatusPromise]).then(values => { Promise.all([tclPromise, velovInfoPromise, velovStatusPromise]).then(values => {
const tcl = values[0]; let tcl = values[0];
const velovInfo = values[1]; let velovInfo = values[1];
let velovStatus = values[2];
let tclResult: ITclFilteredApi|undefined;
const stationsInfo: Record<string, IStationInfo> = {}; const stationsInfo: Record<string, IStationInfo> = {};
if (isError(tcl)) {
console.error(tcl);
tclResult = undefined;
}
else {
tclResult = tcl;
}
const stations: IVelovStation[] = [];
if (isError(velovInfo) || isError(velovStatus)) {
console.error(velovInfo);
console.error(velovStatus);
} else {
velovInfo = velovInfo as IStationsInfoWrapper;
for (const stationInfo of velovInfo.data.stations) { for (const stationInfo of velovInfo.data.stations) {
if (this.monitoredVelovStationIds.includes(stationInfo.station_id)) { if (this.monitoredVelovStationIds.includes(stationInfo.station_id)) {
stationsInfo[stationInfo.station_id] = stationInfo; stationsInfo[stationInfo.station_id] = stationInfo;
} }
} }
const velovStatus = values[2];
velovStatus = velovStatus as IStationsStatusWrapper;
const stationsDict = new Map<string, IVelovStation>(); const stationsDict = new Map<string, IVelovStation>();
for (const stationStatus of velovStatus.data.stations) { for (const stationStatus of velovStatus.data.stations) {
if (this.monitoredVelovStationIds.includes(stationStatus.station_id)) { if (this.monitoredVelovStationIds.includes(stationStatus.station_id)) {
@ -116,30 +133,35 @@ class App extends React.Component<{}, IAppState> {
stationsDict.set(velovStation.info.station_id, velovStation); stationsDict.set(velovStation.info.station_id, velovStation);
} }
} }
const stations: IVelovStation[] = [];
for (const monitoredVelovStationId of this.monitoredVelovStationIds) { for (const monitoredVelovStationId of this.monitoredVelovStationIds) {
const stationInfo = stationsDict.get(monitoredVelovStationId); const stationInfo = stationsDict.get(monitoredVelovStationId);
if (stationInfo) { if (stationInfo) {
stations.push(stationInfo); stations.push(stationInfo);
} }
} }
}
this.setState({ this.setState({
refreshDate: new Date().toLocaleString("fr-fr"), refreshDate: new Date().toLocaleString("fr-fr"),
tcl: tcl, tcl: tclResult,
stations: stations, stations: stations,
}); });
}) })
} }
private reload = () => {
window.location.reload();
}
} }
async function http<T>(request: RequestInfo, init?: RequestInit): Promise<T> { interface ErrorResponse {
detail: string;
}
async function http<T>(request: RequestInfo, init?: RequestInit): Promise<T|ErrorResponse> {
const response = await fetch(request, init); const response = await fetch(request, init);
return await response.json(); return await response.json();
} }
function isError(response: any): response is ErrorResponse {
return response.detail !== undefined;
}
export default App; export default App;