From 4b0f78894530fdee6ba1176ce7e8812edcdfa5c2 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sun, 26 Jan 2020 03:06:37 -0800 Subject: [PATCH] Proper error handling for creating a link without internet (#577) * Proper error handling for creating a link without internet * shuffle code a bit Co-authored-by: David Luzar --- src/scene/data.ts | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/scene/data.ts b/src/scene/data.ts index af27fcf884..65406efbd2 100644 --- a/src/scene/data.ts +++ b/src/scene/data.ts @@ -144,25 +144,31 @@ export async function exportToBackend( elements: readonly ExcalidrawElement[], appState: AppState, ) { - const response = await fetch(BACKEND_POST, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: serializeAsJSON(elements, appState), - }); - const json = await response.json(); - if (json.id) { - const url = new URL(window.location.href); - url.searchParams.append("id", json.id); + let response; + try { + response = await fetch(BACKEND_POST, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: serializeAsJSON(elements, appState), + }); + const json = await response.json(); + if (json.id) { + const url = new URL(window.location.href); + url.searchParams.append("id", json.id); - await navigator.clipboard.writeText(url.toString()); - window.alert( - i18n.t("alerts.copiedToClipboard", { - url: url.toString(), - interpolation: { escapeValue: false }, - }), - ); - } else { + await navigator.clipboard.writeText(url.toString()); + window.alert( + i18n.t("alerts.copiedToClipboard", { + url: url.toString(), + interpolation: { escapeValue: false }, + }), + ); + } else { + window.alert(i18n.t("alerts.couldNotCreateShareableLink")); + } + } catch (e) { window.alert(i18n.t("alerts.couldNotCreateShareableLink")); + return; } }