mirror of
https://gitlab.com/ryzen-controller-team/ryzen-controller.git
synced 2024-12-22 18:13:28 +07:00
fix: Avoid crash when an error exist in preset server response.
This commit is contained in:
parent
40e63d5021
commit
c36c657d04
@ -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 [];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user