From c36c657d04b0d6e05e4dc0a1f9106237b8bfc4dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20=E2=80=9CStorm1er=E2=80=9D=20Decaunes?= Date: Mon, 16 Mar 2020 15:20:20 +0100 Subject: [PATCH] fix: Avoid crash when an error exist in preset server response. --- src/scenes/PresetsScene.tsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/scenes/PresetsScene.tsx b/src/scenes/PresetsScene.tsx index 6ca2e50..4a7b4ba 100644 --- a/src/scenes/PresetsScene.tsx +++ b/src/scenes/PresetsScene.tsx @@ -62,6 +62,12 @@ class PresetsScene extends React.Component<{}, PresetsOnlineContextType> { } updatePresetList() { + function confirmDataPrototype(data: any): data is Array { + if (typeof data?.filter === "function") { + return true; + } + return false; + } this.setState({ loading: true }); const requestOption: RequestInit = { method: "GET", @@ -72,11 +78,15 @@ class PresetsScene extends React.Component<{}, PresetsOnlineContextType> { }; fetch(process.env.REACT_APP_SERVER_ENDPOINT + "/presets", requestOption) .then(response => response.json()) - .then((data: Array) => { + .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 }); }) .catch(reason => { - if (this._isMounted) this.setState({ error: true, loading: false }); + if (this._isMounted) this.setState({ list: [], error: true, loading: false }); + return []; }); }