Improve error handling
This commit is contained in:
parent
97d2d4c460
commit
8f84ffdee5
1 changed files with 48 additions and 26 deletions
74
src/App.tsx
74
src/App.tsx
|
@ -97,49 +97,71 @@ 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> = {};
|
||||||
for (const stationInfo of velovInfo.data.stations) {
|
if (isError(tcl)) {
|
||||||
if (this.monitoredVelovStationIds.includes(stationInfo.station_id)) {
|
console.error(tcl);
|
||||||
stationsInfo[stationInfo.station_id] = stationInfo;
|
tclResult = undefined;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const velovStatus = values[2];
|
else {
|
||||||
const stationsDict = new Map<string, IVelovStation>();
|
tclResult = tcl;
|
||||||
for (const stationStatus of velovStatus.data.stations) {
|
|
||||||
if (this.monitoredVelovStationIds.includes(stationStatus.station_id)) {
|
|
||||||
const velovStation: IVelovStation = {
|
|
||||||
status: stationStatus,
|
|
||||||
info: stationsInfo[stationStatus.station_id]
|
|
||||||
};
|
|
||||||
stationsDict.set(velovStation.info.station_id, velovStation);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const stations: IVelovStation[] = [];
|
const stations: IVelovStation[] = [];
|
||||||
for (const monitoredVelovStationId of this.monitoredVelovStationIds) {
|
if (isError(velovInfo) || isError(velovStatus)) {
|
||||||
const stationInfo = stationsDict.get(monitoredVelovStationId);
|
console.error(velovInfo);
|
||||||
if (stationInfo) {
|
console.error(velovStatus);
|
||||||
stations.push(stationInfo);
|
} else {
|
||||||
|
velovInfo = velovInfo as IStationsInfoWrapper;
|
||||||
|
for (const stationInfo of velovInfo.data.stations) {
|
||||||
|
if (this.monitoredVelovStationIds.includes(stationInfo.station_id)) {
|
||||||
|
stationsInfo[stationInfo.station_id] = stationInfo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
velovStatus = velovStatus as IStationsStatusWrapper;
|
||||||
|
const stationsDict = new Map<string, IVelovStation>();
|
||||||
|
for (const stationStatus of velovStatus.data.stations) {
|
||||||
|
if (this.monitoredVelovStationIds.includes(stationStatus.station_id)) {
|
||||||
|
const velovStation: IVelovStation = {
|
||||||
|
status: stationStatus,
|
||||||
|
info: stationsInfo[stationStatus.station_id]
|
||||||
|
};
|
||||||
|
stationsDict.set(velovStation.info.station_id, velovStation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (const monitoredVelovStationId of this.monitoredVelovStationIds) {
|
||||||
|
const stationInfo = stationsDict.get(monitoredVelovStationId);
|
||||||
|
if (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;
|
||||||
|
|
Loading…
Reference in a new issue