From 69e2c326dad7ff1fbae152f72a2fcd0feadcf94e Mon Sep 17 00:00:00 2001 From: jholdstock Date: Mon, 18 Sep 2023 19:21:05 +0100 Subject: [PATCH] webapi: Attempt cache updat before returning error Rather than immediately returning an error if the cache is not initialized, attempt to initialize it first. --- internal/webapi/middleware.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/internal/webapi/middleware.go b/internal/webapi/middleware.go index 304236be..a0b2e89d 100644 --- a/internal/webapi/middleware.go +++ b/internal/webapi/middleware.go @@ -98,11 +98,14 @@ func (w *WebAPI) withSession(store *sessions.CookieStore) gin.HandlerFunc { // Server Error. func (w *WebAPI) requireWebCache(c *gin.Context) { if !w.cache.initialized() { - const str = "Cache is not yet initialized" - w.log.Errorf(str) - c.String(http.StatusInternalServerError, str) - c.Abort() - return + // Try to initialize it now. + err := w.cache.update() + if err != nil { + w.log.Errorf("Failed to initialize cache: %v", err) + c.String(http.StatusInternalServerError, "Cache is not initialized") + c.Abort() + return + } } c.Set(cacheKey, w.cache.getData())