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 <luzar.david@gmail.com>
This commit is contained in:
Christopher Chedeau 2020-01-26 03:06:37 -08:00 committed by GitHub
parent 1d9cdf4d46
commit 4b0f788945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -144,25 +144,31 @@ export async function exportToBackend(
elements: readonly ExcalidrawElement[], elements: readonly ExcalidrawElement[],
appState: AppState, appState: AppState,
) { ) {
const response = await fetch(BACKEND_POST, { let response;
method: "POST", try {
headers: { "Content-Type": "application/json" }, response = await fetch(BACKEND_POST, {
body: serializeAsJSON(elements, appState), method: "POST",
}); headers: { "Content-Type": "application/json" },
const json = await response.json(); body: serializeAsJSON(elements, appState),
if (json.id) { });
const url = new URL(window.location.href); const json = await response.json();
url.searchParams.append("id", json.id); if (json.id) {
const url = new URL(window.location.href);
url.searchParams.append("id", json.id);
await navigator.clipboard.writeText(url.toString()); await navigator.clipboard.writeText(url.toString());
window.alert( window.alert(
i18n.t("alerts.copiedToClipboard", { i18n.t("alerts.copiedToClipboard", {
url: url.toString(), url: url.toString(),
interpolation: { escapeValue: false }, interpolation: { escapeValue: false },
}), }),
); );
} else { } else {
window.alert(i18n.t("alerts.couldNotCreateShareableLink"));
}
} catch (e) {
window.alert(i18n.t("alerts.couldNotCreateShareableLink")); window.alert(i18n.t("alerts.couldNotCreateShareableLink"));
return;
} }
} }