Add http error handling

Because we are using fetch here to manually request turbo stream we have
to handle errors ourselves.
This commit is contained in:
Gaetan Craig-Riou
2025-09-15 14:23:14 +10:00
parent 01d5830480
commit bc1823e276
2 changed files with 18 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
import { Controller } from "stimulus";
import showHttpError from "../../webpacker/js/services/show_http_error";
export default class extends Controller {
static targets = ["rule", "ruleCustomerTag"];
@@ -29,11 +30,17 @@ export default class extends Controller {
Accept: "text/vnd.turbo-stream.html",
},
})
.then((r) => r.text())
.then((response) => {
if (!response.ok) {
showHttpError(response.status);
throw response;
}
return response.text();
})
.then((html) => {
Turbo.renderStreamMessage(html);
this.indexValue = parseInt(index) + 1;
})
.catch((error) => console.warn(error));
.catch((error) => console.error(error));
}
}

View File

@@ -1,4 +1,5 @@
import { Controller } from "stimulus";
import showHttpError from "../../webpacker/js/services/show_http_error";
export default class extends Controller {
static targets = ["index", "customerRuleIndex"];
@@ -19,11 +20,17 @@ export default class extends Controller {
Accept: "text/vnd.turbo-stream.html",
},
})
.then((r) => r.text())
.then((response) => {
if (!response.ok) {
showHttpError(response.status);
throw response;
}
return response.text();
})
.then((html) => {
Turbo.renderStreamMessage(html);
this.indexTarget.value = parseInt(index) + 1;
})
.catch((error) => console.warn(error));
.catch((error) => console.error(error));
}
}