Skip to content
Snippets Groups Projects
Commit f334fe52 authored by Neuber, Eugen Ramon's avatar Neuber, Eugen Ramon :speech_balloon: Committed by Reiter, Christoph
Browse files

Improve ERROR message on XHR/fetch for non-JSON-LD

See issue VPU/Middleware/API#9
parent 9aee1491
No related branches found
No related tags found
No related merge requests found
......@@ -10,23 +10,35 @@ import {i18n} from "./i18n";
* @param icon
*/
export const handleXhrError = (jqXHR, textStatus, errorThrown, icon = "sad") => {
if (textStatus !== "abort") {
// try to show hydra error text
let body = jqXHR.responseJSON !== undefined && jqXHR.responseJSON["hydra:description"] !== undefined ?
jqXHR.responseJSON["hydra:description"] : textStatus;
// return if user aborted the request
if (textStatus === "abort") {
return;
}
// if the server is not reachable at all
if (jqXHR.status === 0) {
body = i18n.t('error.connection-to-server-refused');
}
let body;
notify({
"summary": i18n.t('error.summary'),
"body": body,
"icon": icon,
"type": "danger",
});
if (jqXHR.responseJSON !== undefined && jqXHR.responseJSON["hydra:description"] !== undefined) {
// response is a JSON-LD
body = jqXHR.responseJSON["hydra:description"];
} else if (jqXHR.responseJSON !== undefined && jqXHR.responseJSON['detail'] !== undefined) {
// response is a plain JSON
body = jqXHR.responseJSON['detail'];
} else {
// no description available
body = textStatus;
}
// if the server is not reachable at all
if (jqXHR.status === 0) {
body = i18n.t('error.connection-to-server-refused');
}
notify({
"summary": i18n.t('error.summary'),
"body": body,
"icon": icon,
"type": "danger",
});
};
/**
......@@ -46,7 +58,16 @@ export const handleFetchError = async (error, summary = "", icon = "sad") => {
try {
await error.json().then((json) => {
body = json["hydra:description"] !== undefined ? json["hydra:description"] : error.statusText;
if (json["hydra:description"] !== undefined) {
// response is a JSON-LD
body = json["hydra:description"];
} else if(json['detail'] !== undefined) {
// response is a plain JSON
body = json['detail'];
} else {
// no description available
body = error.statusText;
}
}).catch(() => {
body = error.statusText !== undefined ? error.statusText : error;
});
......
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