add various missing error handling

This commit is contained in:
2026-05-27 18:43:27 +00:00
parent 26a21e6cc7
commit 08cd4dfaaa
4 changed files with 54 additions and 20 deletions
+8 -2
View File
@@ -47,7 +47,7 @@ func InitMinify() *minify.M {
func MinifyStatic(m *minify.M, files embed.FS) map[string]StaticFile { func MinifyStatic(m *minify.M, files embed.FS) map[string]StaticFile {
minified := make(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 { if err != nil {
return err return err
} }
@@ -84,7 +84,13 @@ func MinifyStatic(m *minify.M, files embed.FS) map[string]StaticFile {
} }
return nil return nil
}) })
if err != nil {
log.Printf("[Error] MinifyStatic: %s", err)
return nil
} else {
return minified return minified
}
} }
func LoadHTMLToGin(engine *gin.Engine, html map[string]StaticFile) *template.Template { 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 return nil, 0, err
} }
for k, v := range context.Cookies { 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{} client := &http.Client{}
+5 -1
View File
@@ -64,10 +64,14 @@ func HandleGETBackupsFragment(c *gin.Context) {
} }
c.Header("Content-Type", "text/plain") 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, "backups": backups,
}) })
if err != nil {
c.Status(http.StatusInternalServerError)
} else {
c.Status(http.StatusOK) c.Status(http.StatusOK)
}
} else { // return 401 } else { // return 401
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
} }
+20 -4
View File
@@ -84,10 +84,14 @@ func HandleGETConfigVolumesFragment(c *gin.Context) {
} }
c.Header("Content-Type", "text/plain") 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, "config": config,
}) })
if err != nil {
c.Status(http.StatusInternalServerError)
} else {
c.Status(http.StatusOK) c.Status(http.StatusOK)
}
} else { } else {
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
} }
@@ -108,10 +112,14 @@ func HandleGETConfigNetsFragment(c *gin.Context) {
} }
c.Header("Content-Type", "text/plain") 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, "config": config,
}) })
if err != nil {
c.Status(http.StatusInternalServerError)
} else {
c.Status(http.StatusOK) c.Status(http.StatusOK)
}
} else { } else {
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
} }
@@ -132,10 +140,14 @@ func HandleGETConfigDevicesFragment(c *gin.Context) {
} }
c.Header("Content-Type", "text/plain") 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, "config": config,
}) })
if err != nil {
c.Status(http.StatusInternalServerError)
} else {
c.Status(http.StatusOK) c.Status(http.StatusOK)
}
} else { } else {
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
} }
@@ -156,10 +168,14 @@ func HandleGETConfigBootFragment(c *gin.Context) {
} }
c.Header("Content-Type", "text/plain") 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, "config": config,
}) })
if err != nil {
c.Status(http.StatusInternalServerError)
} else {
c.Status(http.StatusOK) c.Status(http.StatusOK)
}
} else { } else {
c.Status(http.StatusUnauthorized) c.Status(http.StatusUnauthorized)
} }
+14 -6
View File
@@ -72,10 +72,14 @@ func HandleGETInstancesFragment(c *gin.Context) {
return return
} }
c.Header("Content-Type", "text/plain") 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, "instances": instances,
}) })
if err != nil {
c.Status(http.StatusInternalServerError)
} else {
c.Status(http.StatusOK) c.Status(http.StatusOK)
}
} else { // return 401 } else { // return 401
c.Status(http.StatusUnauthorized) 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{} 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 // iterate through recent user accessible tasks to find the task most recently made on an instance
for _, v := range body { 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 most_recent_task[task.VMID] = task.EndTime // update the most recent task
switch task.Type { switch task.Type {
case "qmstart", "vzstart": // if the task was a start task, update the expected state to running 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 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 // 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 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 // get /status/current which is updated faster than /cluster/resources
instance := instances[vmid] 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) return nil, nil, fmt.Errorf("request to %s resulted in %+v", path, res)
} }
// attempt to decode task status as instance status
status := InstanceStatus{} 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 instance.Status = status.Status
instances[vmid] = instance instances[vmid] = instance