From 737e2f6425e70137a4c9fa134b1ee68cfe1cfcbf Mon Sep 17 00:00:00 2001 From: Michael Geers Date: Thu, 27 Feb 2025 20:34:11 +0100 Subject: [PATCH] Plan UI: handle plan not found (#19245) --- .../js/components/ChargingPlansSettings.vue | 23 +++++++++++++++---- server/http_loadpoint_handler.go | 6 ++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/assets/js/components/ChargingPlansSettings.vue b/assets/js/components/ChargingPlansSettings.vue index 14601e5b0..466a7ee1e 100644 --- a/assets/js/components/ChargingPlansSettings.vue +++ b/assets/js/components/ChargingPlansSettings.vue @@ -207,7 +207,7 @@ export default { }, fetchActivePlan: async function () { try { - const res = await api.get(`loadpoints/${this.id}/plan`); + const res = await this.apiFetchPlan(`loadpoints/${this.id}/plan`); this.plan = res.data.result; this.nextPlanId = this.plan.planId; } catch (e) { @@ -217,19 +217,34 @@ export default { }, fetchStaticPreviewSoc: async function (soc, time) { const timeISO = time.toISOString(); - return await api.get(`loadpoints/${this.id}/plan/static/preview/soc/${soc}/${timeISO}`); + return await this.apiFetchPlan( + `loadpoints/${this.id}/plan/static/preview/soc/${soc}/${timeISO}` + ); }, fetchRepeatingPreview: async function (weekdays, soc, time, tz) { - return await api.get( + return await this.apiFetchPlan( `loadpoints/${this.id}/plan/repeating/preview/${soc}/${weekdays}/${time}/${encodeURIComponent(tz)}` ); }, fetchStaticPreviewEnergy: async function (energy, time) { const timeISO = time.toISOString(); - return await api.get( + return await this.apiFetchPlan( `loadpoints/${this.id}/plan/static/preview/energy/${energy}/${timeISO}` ); }, + apiFetchPlan: async function (url) { + try { + const res = await api.get(url, { + validateStatus: (code) => [200, 404].includes(code), + }); + if (res.status === 404) { + return { data: { result: {} } }; + } + return res; + } catch (e) { + console.error(e); + } + }, fetchPreviewPlan: async function () { // only show preview of no plan is active if (!this.noActivePlan) return; diff --git a/server/http_loadpoint_handler.go b/server/http_loadpoint_handler.go index a7def3467..80e3ab650 100644 --- a/server/http_loadpoint_handler.go +++ b/server/http_loadpoint_handler.go @@ -52,7 +52,7 @@ func planHandler(lp loadpoint.API) http.HandlerFunc { requiredDuration := lp.GetPlanRequiredDuration(goal, maxPower) plan := lp.GetPlan(planTime, requiredDuration) if plan == nil { - w.WriteHeader(http.StatusBadRequest) + w.WriteHeader(http.StatusNotFound) return } @@ -111,7 +111,7 @@ func staticPlanPreviewHandler(lp loadpoint.API) http.HandlerFunc { requiredDuration := lp.GetPlanRequiredDuration(goal, maxPower) plan := lp.GetPlan(planTime, requiredDuration) if plan == nil { - w.WriteHeader(http.StatusBadRequest) + w.WriteHeader(http.StatusNotFound) return } @@ -164,7 +164,7 @@ func repeatingPlanPreviewHandler(lp loadpoint.API) http.HandlerFunc { requiredDuration := lp.GetPlanRequiredDuration(soc, maxPower) plan := lp.GetPlan(planTime, requiredDuration) if plan == nil { - w.WriteHeader(http.StatusBadRequest) + w.WriteHeader(http.StatusNotFound) return }