mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-07 22:46:06 +00:00
Move turbo http error handling to its own file
This commit is contained in:
12
app/webpacker/js/services/show_http_error.js
Normal file
12
app/webpacker/js/services/show_http_error.js
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
// Display an alert to the user based on the http status
|
||||||
|
|
||||||
|
export default function showHttpError(status) {
|
||||||
|
// Note that other 4xx errors will be handled differently.
|
||||||
|
if (status == 401) {
|
||||||
|
alert(I18n.t("errors.unauthorized.message"));
|
||||||
|
} else if (status === undefined) {
|
||||||
|
alert(I18n.t("errors.network_error.message"));
|
||||||
|
} else if (status >= 500) {
|
||||||
|
alert(I18n.t("errors.general_error.message"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import "@hotwired/turbo";
|
import "@hotwired/turbo";
|
||||||
|
import showHttpError from "./services/show_http_error";
|
||||||
|
|
||||||
import TurboPower from "turbo_power";
|
import TurboPower from "turbo_power";
|
||||||
TurboPower.initialize(Turbo.StreamActions);
|
TurboPower.initialize(Turbo.StreamActions);
|
||||||
@@ -12,20 +13,9 @@ document.addEventListener("turbo:frame-missing", (event) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener("turbo:submit-end", (event) => {
|
document.addEventListener("turbo:submit-end", (event) => {
|
||||||
if (!event.detail.success){
|
if (!event.detail.success) {
|
||||||
// show error message on failure
|
// show error message on failure
|
||||||
showError(event.detail.fetchResponse?.statusCode);
|
showHttpError(event.detail.fetchResponse?.statusCode);
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function showError(status) {
|
|
||||||
// Note that other 4xx errors will be handled differently.
|
|
||||||
if(status == 401) {
|
|
||||||
alert(I18n.t("errors.unauthorized.message"));
|
|
||||||
} else if(status === undefined) {
|
|
||||||
alert(I18n.t("errors.network_error.message"));
|
|
||||||
} else if (status >= 500) {
|
|
||||||
alert(I18n.t("errors.general_error.message"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user