From 0a284adc18ef87e00b5b3c9431ae406b3e28a714 Mon Sep 17 00:00:00 2001 From: Kostas Bariotis Date: Fri, 10 Apr 2020 10:58:09 +0100 Subject: [PATCH] Allow opening empty excalidraw file (#1348) * allow openning empty file * correctly throw error * fix error handling * switch back to error objects Co-authored-by: dwelle --- src/actions/actionExport.tsx | 2 +- src/components/App.tsx | 2 +- src/data/blob.ts | 11 +++-------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/actions/actionExport.tsx b/src/actions/actionExport.tsx index bfe57d64..54d832f8 100644 --- a/src/actions/actionExport.tsx +++ b/src/actions/actionExport.tsx @@ -92,7 +92,7 @@ export const actionLoadScene = register({ updateData({ elements: elements, appState: appState }); }) .catch((error) => { - updateData({ error: error }); + updateData({ error: error.message }); }); }} /> diff --git a/src/components/App.tsx b/src/components/App.tsx index d95da0e1..4c3be550 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -2358,7 +2358,7 @@ export class App extends React.Component { }), ) .catch((error) => { - this.setState({ isLoading: false, errorMessage: error }); + this.setState({ isLoading: false, errorMessage: error.message }); }); } else { this.setState({ diff --git a/src/data/blob.ts b/src/data/blob.ts index 53204de1..7d36208a 100644 --- a/src/data/blob.ts +++ b/src/data/blob.ts @@ -1,5 +1,4 @@ import { getDefaultAppState } from "../appState"; -import { DataState } from "./types"; import { restore } from "./restore"; import { t } from "../i18n"; @@ -16,7 +15,7 @@ export async function loadFromBlob(blob: any) { elements = data.elements || []; appState = { ...defaultAppState, ...data.appState }; } catch { - // Do nothing because elements array is already empty + throw new Error(t("alerts.couldNotLoadInvalidFile")); } return { elements, appState }; }; @@ -38,11 +37,7 @@ export async function loadFromBlob(blob: any) { }; }); } + const { elements, appState } = updateAppState(contents); - if (!elements.length) { - return Promise.reject(t("alerts.couldNotLoadInvalidFile")); - } - return new Promise((resolve) => { - resolve(restore(elements, appState, { scrollToContent: true })); - }); + return restore(elements, appState, { scrollToContent: true }); }