Remove (almost) all the panics
This commit is contained in:
parent
3dec93c4f4
commit
d9a979b782
@ -319,7 +319,8 @@ func (cs *ConfigService) LoadConfig() error {
|
||||
func (cs *ConfigService) WriteConfig() {
|
||||
s, err := yaml.Marshal(cs.Config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error writing config: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
path := cs.ConfigPath
|
||||
|
@ -429,8 +429,6 @@ func (dl *Download) updateMetadata(s string) {
|
||||
p, err := strconv.ParseFloat(matches[1], 32)
|
||||
if err == nil {
|
||||
dl.Percent = float32(p)
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -317,7 +317,7 @@ func TestUpdateMetadataSingle(t *testing.T) {
|
||||
[youtube] 2WoDQBhJCVQ: Downloading android player API JSON
|
||||
[info] 2WoDQBhJCVQ: Downloading 1 format(s): 137+140
|
||||
[info] Writing video metadata as JSON to: The Greatest Shot In Television [2WoDQBhJCVQ].info.json
|
||||
[download] Destination: The Greatest Shot In Television [2WoDQBhJCVQ].f137.mp4
|
||||
[debug] Invoking hlsnative downloader on "https://example.org/urls/1.2.3.4%
|
||||
[download] 0.0% of 12.82MiB at 510.94KiB/s ETA 00:26
|
||||
[download] 0.0% of 12.82MiB at 966.50KiB/s ETA 00:13
|
||||
[download] 0.1% of 12.82MiB at 1.54MiB/s ETA 00:08
|
||||
|
65
web/web.go
65
web/web.go
@ -92,7 +92,10 @@ func homeHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Man
|
||||
|
||||
t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/menu.tmpl", "data/templates/index.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
type Info struct {
|
||||
@ -113,7 +116,10 @@ func homeHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Man
|
||||
defer dm.Lock.Unlock()
|
||||
err = t.ExecuteTemplate(w, "layout", info)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -149,12 +155,18 @@ func configHandler() func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/menu.tmpl", "data/templates/config.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
err = t.ExecuteTemplate(w, "layout", nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -167,7 +179,10 @@ func configRESTHandler(cs *config.ConfigService) func(w http.ResponseWriter, r *
|
||||
log.Printf("Updating config")
|
||||
b, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
err = cs.Config.UpdateFromJSON(b)
|
||||
|
||||
@ -221,7 +236,10 @@ func fetchInfoOneRESTHandler(cs *config.ConfigService, dm *download.Manager) fun
|
||||
|
||||
b, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(b, &thisReq)
|
||||
@ -271,7 +289,10 @@ func fetchInfoRESTHandler(dm *download.Manager) func(w http.ResponseWriter, r *h
|
||||
|
||||
b, err := dm.DownloadsAsJSON()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
_, err = w.Write(b)
|
||||
if err != nil {
|
||||
@ -307,14 +328,20 @@ func fetchHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Ma
|
||||
|
||||
t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/popup.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
templateData := map[string]interface{}{"dl": dl, "config": cs.Config, "canStop": download.CanStopDownload, "Version": vm.GetInfo()}
|
||||
|
||||
err = t.ExecuteTemplate(w, "layout", templateData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
return
|
||||
} else if method == "POST" {
|
||||
@ -389,13 +416,19 @@ func fetchHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Ma
|
||||
|
||||
t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/popup_create.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
templateData := map[string]interface{}{"config": cs.Config, "url": url[0], "Version": vm.GetInfo()}
|
||||
|
||||
err = t.ExecuteTemplate(w, "layout", templateData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
@ -412,13 +445,19 @@ func bulkHandler(cs *config.ConfigService, vm *version.Manager, dm *download.Man
|
||||
|
||||
t, err := template.ParseFS(webFS, "data/templates/layout.tmpl", "data/templates/menu.tmpl", "data/templates/bulk.tmpl")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
templateData := map[string]interface{}{"config": cs.Config, "Version": vm.GetInfo()}
|
||||
|
||||
err = t.ExecuteTemplate(w, "layout", templateData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Printf("error: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
|
Loading…
x
Reference in New Issue
Block a user