From 08cd4dfaaa9a51a1db122ee0af80ccb027425a46 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 27 May 2026 18:43:27 +0000 Subject: [PATCH] add various missing error handling --- app/common/utils.go | 12 +++++++++--- app/routes/backups.go | 8 ++++++-- app/routes/config.go | 32 ++++++++++++++++++++++++-------- app/routes/index.go | 22 +++++++++++++++------- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/app/common/utils.go b/app/common/utils.go index acbd91b..387d617 100644 --- a/app/common/utils.go +++ b/app/common/utils.go @@ -47,7 +47,7 @@ func InitMinify() *minify.M { func MinifyStatic(m *minify.M, files embed.FS) map[string]StaticFile { minified := make(map[string]StaticFile) - fs.WalkDir(files, ".", func(path string, entry fs.DirEntry, err error) error { + err := fs.WalkDir(files, ".", func(path string, entry fs.DirEntry, err error) error { if err != nil { return err } @@ -84,7 +84,13 @@ func MinifyStatic(m *minify.M, files embed.FS) map[string]StaticFile { } return nil }) - return minified + + if err != nil { + log.Printf("[Error] MinifyStatic: %s", err) + return nil + } else { + return minified + } } func LoadHTMLToGin(engine *gin.Engine, html map[string]StaticFile) *template.Template { @@ -165,7 +171,7 @@ func RequestGetAPI(path string, context RequestContext, body any) (*http.Respons return nil, 0, err } for k, v := range context.Cookies { - req.AddCookie(&http.Cookie{Name: k, Value: v}) + req.AddCookie(&http.Cookie{Name: k, Value: v, Secure: true}) } client := &http.Client{} diff --git a/app/routes/backups.go b/app/routes/backups.go index 944d299..481822a 100644 --- a/app/routes/backups.go +++ b/app/routes/backups.go @@ -64,10 +64,14 @@ func HandleGETBackupsFragment(c *gin.Context) { } c.Header("Content-Type", "text/plain") - common.TMPL.ExecuteTemplate(c.Writer, "html/backups-backups.go.tmpl", gin.H{ + err = common.TMPL.ExecuteTemplate(c.Writer, "html/backups-backups.go.tmpl", gin.H{ "backups": backups, }) - c.Status(http.StatusOK) + if err != nil { + c.Status(http.StatusInternalServerError) + } else { + c.Status(http.StatusOK) + } } else { // return 401 c.Status(http.StatusUnauthorized) } diff --git a/app/routes/config.go b/app/routes/config.go index c43d6a3..83191c7 100644 --- a/app/routes/config.go +++ b/app/routes/config.go @@ -84,10 +84,14 @@ func HandleGETConfigVolumesFragment(c *gin.Context) { } c.Header("Content-Type", "text/plain") - common.TMPL.ExecuteTemplate(c.Writer, "html/config-volumes.go.tmpl", gin.H{ + err = common.TMPL.ExecuteTemplate(c.Writer, "html/config-volumes.go.tmpl", gin.H{ "config": config, }) - c.Status(http.StatusOK) + if err != nil { + c.Status(http.StatusInternalServerError) + } else { + c.Status(http.StatusOK) + } } else { c.Status(http.StatusUnauthorized) } @@ -108,10 +112,14 @@ func HandleGETConfigNetsFragment(c *gin.Context) { } c.Header("Content-Type", "text/plain") - common.TMPL.ExecuteTemplate(c.Writer, "html/config-nets.go.tmpl", gin.H{ + err = common.TMPL.ExecuteTemplate(c.Writer, "html/config-nets.go.tmpl", gin.H{ "config": config, }) - c.Status(http.StatusOK) + if err != nil { + c.Status(http.StatusInternalServerError) + } else { + c.Status(http.StatusOK) + } } else { c.Status(http.StatusUnauthorized) } @@ -132,10 +140,14 @@ func HandleGETConfigDevicesFragment(c *gin.Context) { } c.Header("Content-Type", "text/plain") - common.TMPL.ExecuteTemplate(c.Writer, "html/config-devices.go.tmpl", gin.H{ + err = common.TMPL.ExecuteTemplate(c.Writer, "html/config-devices.go.tmpl", gin.H{ "config": config, }) - c.Status(http.StatusOK) + if err != nil { + c.Status(http.StatusInternalServerError) + } else { + c.Status(http.StatusOK) + } } else { c.Status(http.StatusUnauthorized) } @@ -156,10 +168,14 @@ func HandleGETConfigBootFragment(c *gin.Context) { } c.Header("Content-Type", "text/plain") - common.TMPL.ExecuteTemplate(c.Writer, "html/config-boot.go.tmpl", gin.H{ + err = common.TMPL.ExecuteTemplate(c.Writer, "html/config-boot.go.tmpl", gin.H{ "config": config, }) - c.Status(http.StatusOK) + if err != nil { + c.Status(http.StatusInternalServerError) + } else { + c.Status(http.StatusOK) + } } else { c.Status(http.StatusUnauthorized) } diff --git a/app/routes/index.go b/app/routes/index.go index 94e27e1..52ddfc8 100644 --- a/app/routes/index.go +++ b/app/routes/index.go @@ -72,10 +72,14 @@ func HandleGETInstancesFragment(c *gin.Context) { return } c.Header("Content-Type", "text/plain") - common.TMPL.ExecuteTemplate(c.Writer, "html/index-instances.go.tmpl", gin.H{ + err = common.TMPL.ExecuteTemplate(c.Writer, "html/index-instances.go.tmpl", gin.H{ "instances": instances, }) - c.Status(http.StatusOK) + if err != nil { + c.Status(http.StatusInternalServerError) + } else { + c.Status(http.StatusOK) + } } else { // return 401 c.Status(http.StatusUnauthorized) } @@ -145,7 +149,7 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No } most_recent_task := map[uint]uint{} - expected_state := map[uint]string{} + expected_states := map[uint]string{} // iterate through recent user accessible tasks to find the task most recently made on an instance for _, v := range body { @@ -179,16 +183,16 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No most_recent_task[task.VMID] = task.EndTime // update the most recent task switch task.Type { case "qmstart", "vzstart": // if the task was a start task, update the expected state to running - expected_state[task.VMID] = "running" + expected_states[task.VMID] = "running" case "qmstop", "vzstop": // if the task was a stop task, update the expected state to stopped - expected_state[task.VMID] = "stopped" + expected_states[task.VMID] = "stopped" } } } } // iterate through the instances with recent tasks, refetch their state from a more reliable source - for vmid, expected_state := range expected_state { // for the expected states from recent tasks + for vmid, expected_state := range expected_states { // for the expected states from recent tasks if instances[vmid].Status != expected_state { // if the current node's state from /cluster/resources differs from expected state // get /status/current which is updated faster than /cluster/resources instance := instances[vmid] @@ -202,8 +206,12 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No return nil, nil, fmt.Errorf("request to %s resulted in %+v", path, res) } + // attempt to decode task status as instance status status := InstanceStatus{} - mapstructure.Decode(body, &status) + err = mapstructure.Decode(body, &status) + if err != nil { // did not successfully decode task status, just skip + continue + } instance.Status = status.Status instances[vmid] = instance