From 9df2aef82d3ea79c3571cab474b2b9217c58bc34 Mon Sep 17 00:00:00 2001 From: keiya01 <34934510+keiya01@users.noreply.github.com> Date: Wed, 17 Jul 2024 11:44:29 +0900 Subject: [PATCH] fix(extension): suppress 404 error --- extension/src/shared/api/clients/base.ts | 34 ++++++++++++++----- extension/src/shared/api/hooks/settings.ts | 2 +- extension/src/shared/api/hooks/template.ts | 2 +- extension/src/shared/view-layers/rootLayer.ts | 5 +-- .../src/toolbar/containers/InitializeApp.tsx | 4 +-- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/extension/src/shared/api/clients/base.ts b/extension/src/shared/api/clients/base.ts index f59ed7750..ec81aed51 100644 --- a/extension/src/shared/api/clients/base.ts +++ b/extension/src/shared/api/clients/base.ts @@ -45,27 +45,45 @@ export class PlateauAPIClient { } handleError(obj: any) { - if (!obj) return []; - if (typeof obj === "object" && "error" in obj) return []; + if (!obj) return; + if (typeof obj !== "object") return; + if ("error" in obj) { + console.error(obj.error); + return; + } return obj; } // Combine the data only in findAll. - async findAll(): Promise { + async findAll(): Promise { return this.handleError( await Promise.all<(Promise | undefined)[]>([ fetchWithGet(this.baseUrl()), this.cityOptions ? fetchWithGet(this.baseUrlForCity()) : undefined, - ]).then(r => r.filter((v): v is V[] => !!v).flat()), + ]) + .then(r => r.filter((v): v is V[] => !!v).flat()) + .catch(e => { + console.error(e); + return []; + }), ); } - async findAllForCity(): Promise { - return this.handleError(await fetchWithGet(this.baseUrlForCity())); + async findAllForCity(): Promise { + return this.handleError( + await fetchWithGet(this.baseUrlForCity()).catch(e => { + console.error(e); + return []; + }), + ); } - async findById(id: string): Promise { - return this.handleError(await fetchWithGet(this.urlWithId(id))); + async findById(id: string): Promise { + return this.handleError( + await fetchWithGet(this.urlWithId(id)).catch(e => { + console.error(e); + }), + ); } async save(data: V) { diff --git a/extension/src/shared/api/hooks/settings.ts b/extension/src/shared/api/hooks/settings.ts index 391064a5d..401465009 100644 --- a/extension/src/shared/api/hooks/settings.ts +++ b/extension/src/shared/api/hooks/settings.ts @@ -16,7 +16,7 @@ export default () => { setIsSaving(true); const settings = await client.findAll(); - const existSetting = settings.find( + const existSetting = settings?.find( s => s.datasetId === setting.datasetId && s.dataId === setting.dataId, ); diff --git a/extension/src/shared/api/hooks/template.ts b/extension/src/shared/api/hooks/template.ts index 25cc9f86e..c5d96eb04 100644 --- a/extension/src/shared/api/hooks/template.ts +++ b/extension/src/shared/api/hooks/template.ts @@ -22,7 +22,7 @@ export default () => { useEffect(() => { if (isCityProject) { client.findAllForCity().then(data => { - setTemplates(data); + setTemplates(data ?? []); }); } else { setTemplates(templatesFromAtom); diff --git a/extension/src/shared/view-layers/rootLayer.ts b/extension/src/shared/view-layers/rootLayer.ts index f6a55b942..2c1a83d4f 100644 --- a/extension/src/shared/view-layers/rootLayer.ts +++ b/extension/src/shared/view-layers/rootLayer.ts @@ -163,7 +163,7 @@ const findComponentTemplate = ( // Default template const templateWithName = dataName - ? templates.find(t => [dataName, dataSubName].includes(t.name.split("/").slice(-1)[0])) + ? templates.find(t => [dataName, dataSubName].includes(t.name?.split("/").slice(-1)[0])) : undefined; const template = @@ -186,7 +186,8 @@ const findEmphasisProperties = ( const templateWithName = dataName ? templates.find( t => - t.type === "emphasis" && [dataName, dataSubName].includes(t.name.split("/").slice(-1)[0]), + t.type === "emphasis" && + [dataName, dataSubName].includes(t.name?.split("/").slice(-1)[0]), ) : undefined; diff --git a/extension/src/toolbar/containers/InitializeApp.tsx b/extension/src/toolbar/containers/InitializeApp.tsx index ecbae82f0..4df470f43 100644 --- a/extension/src/toolbar/containers/InitializeApp.tsx +++ b/extension/src/toolbar/containers/InitializeApp.tsx @@ -30,8 +30,8 @@ export const InitializeApp: FC = () => { settingClient.findAll(), templateClient.findAll(), ]); - updateAllSetting(settings); - updateAllTemplate(Array.isArray(templates) ? templates : []); + updateAllSetting(settings ?? []); + updateAllTemplate(templates ?? []); setIsAppReady(true); }; fetch();