Skip to content
Snippets Groups Projects
Commit 04e8cfe3 authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire: Committed by Reiter, Christoph
Browse files

Rename xhrError to handleXhrError and add handleFetchError

parent 292a98a6
No related branches found
No related tags found
No related merge requests found
import {send as notify} from './notification'; import {send as notify} from './notification';
import {i18n} from "./i18n"; 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") { if (textStatus !== "abort") {
// try to show hydra error text // try to show hydra error text
let body = jqXHR.responseJSON !== undefined && jqXHR.responseJSON["hydra:description"] !== undefined ? let body = jqXHR.responseJSON !== undefined && jqXHR.responseJSON["hydra:description"] !== undefined ?
...@@ -19,3 +26,32 @@ export const xhrError = (jqXHR, textStatus, errorThrown) => { ...@@ -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",
});
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment