This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/lib/ajax-error.js
Jarek Radosz fe5923da06
DEV: Do not re-throw in popupAjaxError (#13462)
Effectively reverts 3ddc33b07c

Makes the failure states testable; see the uncommented test.

I don't think we're re-catching these errors anyway?

_update:_
We did in a single instance in discourse-code-review but it wasn't really intentional and I fixed it in https://github.com/discourse/discourse-code-review/pull/73
2021-06-22 19:29:20 +02:00

69 lines
1.6 KiB
JavaScript

import I18n from "I18n";
import bootbox from "bootbox";
export function extractError(error, defaultMessage) {
if (error instanceof Error) {
// eslint-disable-next-line no-console
console.error(error.stack);
}
if (typeof error === "string") {
// eslint-disable-next-line no-console
console.error(error);
}
if (error.jqXHR) {
error = error.jqXHR;
}
let parsedError, parsedJSON;
if (error.responseJSON) {
parsedJSON = error.responseJSON;
}
if (!parsedJSON && error.responseText) {
try {
parsedJSON = $.parseJSON(error.responseText);
} catch (ex) {
// in case the JSON doesn't parse
// eslint-disable-next-line no-console
console.error(ex.stack);
}
}
if (parsedJSON) {
if (parsedJSON.errors && parsedJSON.errors.length > 0) {
parsedError = parsedJSON.errors.join("<br>");
} else if (parsedJSON.error) {
parsedError = parsedJSON.error;
} else if (parsedJSON.message) {
parsedError = parsedJSON.message;
} else if (parsedJSON.failed) {
parsedError = parsedJSON.failed;
}
}
if (!parsedError) {
if (error.status && error.status >= 400) {
parsedError = error.status + " " + error.statusText;
}
}
return parsedError || defaultMessage || I18n.t("generic_error");
}
export function throwAjaxError(undoCallback) {
return function (error) {
// If we provided an `undo` callback
if (undoCallback) {
undoCallback(error);
}
throw extractError(error);
};
}
export function popupAjaxError(error) {
bootbox.alert(extractError(error));
}