From 04e8cfe31f3665349f642c4be32dd6ce1b559275 Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle <patrizio.bekerle@tugraz.at> Date: Wed, 11 Sep 2019 14:32:50 +0200 Subject: [PATCH] Rename xhrError to handleXhrError and add handleFetchError --- packages/common/error.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/common/error.js b/packages/common/error.js index 36b7ed54..3c14abae 100644 --- a/packages/common/error.js +++ b/packages/common/error.js @@ -1,7 +1,14 @@ import {send as notify} from './notification'; import {i18n} from "./i18n"; -export const xhrError = (jqXHR, textStatus, errorThrown) => { +/** + * Error handling for XHR errors + * + * @param jqXHR + * @param textStatus + * @param errorThrown + */ +export const handleXhrError = (jqXHR, textStatus, errorThrown) => { if (textStatus !== "abort") { // try to show hydra error text let body = jqXHR.responseJSON !== undefined && jqXHR.responseJSON["hydra:description"] !== undefined ? @@ -19,3 +26,32 @@ export const xhrError = (jqXHR, textStatus, errorThrown) => { }); } }; + +/** + * Error handling for fetch errors + * + * @param error + * @param summary + */ +export const handleFetchError = async (error, summary = "") => { + let body; + + try { + await error.json().then((json) => { + body = json["hydra:description"] !== undefined ? json["hydra:description"] : error.statusText; + }).catch(() => { + body = error.statusText !== undefined ? error.statusText : error; + }); + } catch (e) { + // we get a TypeError if the connection to the server was refused + if (error.name === "TypeError") { + body = i18n.t('error.connection-to-server-refused'); + } + } + + notify({ + "summary": summary === "" ? i18n.t('error.summary') : summary, + "body": body, + "type": "danger", + }); +}; -- GitLab