fix: Avoid crash when an error exist in preset server response.

This commit is contained in:
Quentin “Storm1er” Decaunes 2020-03-16 15:20:20 +01:00
parent 40e63d5021
commit c36c657d04

View File

@ -62,6 +62,12 @@ class PresetsScene extends React.Component<{}, PresetsOnlineContextType> {
} }
updatePresetList() { updatePresetList() {
function confirmDataPrototype(data: any): data is Array<ApiPreset> {
if (typeof data?.filter === "function") {
return true;
}
return false;
}
this.setState({ loading: true }); this.setState({ loading: true });
const requestOption: RequestInit = { const requestOption: RequestInit = {
method: "GET", method: "GET",
@ -72,11 +78,15 @@ class PresetsScene extends React.Component<{}, PresetsOnlineContextType> {
}; };
fetch(process.env.REACT_APP_SERVER_ENDPOINT + "/presets", requestOption) fetch(process.env.REACT_APP_SERVER_ENDPOINT + "/presets", requestOption)
.then(response => response.json()) .then(response => response.json())
.then((data: Array<ApiPreset>) => { .then(data => {
if (!confirmDataPrototype(data)) {
throw new Error("Preset list from API is malformed.");
}
if (this._isMounted) this.setState({ list: data, loading: false, error: false }); if (this._isMounted) this.setState({ list: data, loading: false, error: false });
}) })
.catch(reason => { .catch(reason => {
if (this._isMounted) this.setState({ error: true, loading: false }); if (this._isMounted) this.setState({ list: [], error: true, loading: false });
return [];
}); });
} }