From 33fe018232e1d20287b8dcc19e10bd4a40757b99 Mon Sep 17 00:00:00 2001 From: balki <3070606-balki@users.noreply.gitlab.com> Date: Fri, 24 Jun 2022 23:43:38 -0400 Subject: [PATCH] cleanup errors --- main.go | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 8053fbf..c662459 100644 --- a/main.go +++ b/main.go @@ -52,7 +52,8 @@ func parse() { } func main() { - fmt.Println("vim-go") + log.Print("Youtube UI") + log.SetFlags(log.Flags() | log.Lshortfile) parse() tmpl := template.New("page") tmpl = tmpl.Funcs(map[string]any{ @@ -60,11 +61,11 @@ func main() { }) tmpl, err := tmpl.Parse(page) if err != nil { - panic(err) + log.Panic(err) } d, err = db.Load(dbPath) if err != nil { - panic(err) + log.Panic(err) } defer d.Save() seen := map[string]struct{}{} @@ -77,17 +78,16 @@ func main() { if r.Method == http.MethodGet { d.Run(func(d *db.Jdb) { if err := tmpl.Execute(w, d); err != nil { - panic(err) + log.Panic(err) } }) return } - if err := r.ParseForm(); err != nil { - panic(err) - } yturl := r.PostFormValue("youtube_url") if yturl == "" { - panic("yturl empty") + log.Printf("yturl empty, postform:%v\n", r.PostForm) + w.WriteHeader(http.StatusInternalServerError) + return } if _, ok := seen[yturl]; !ok { seen[yturl] = struct{}{} @@ -104,18 +104,21 @@ func main() { } http.Redirect(w, r, "/", http.StatusSeeOther) }) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) + log.Panic(http.ListenAndServe(fmt.Sprintf(":%v", port), nil)) } func getTitle(id int, yturl string) { args := append(ytdlCmd, "--get-title", yturl) cmd := exec.Command(args[0], args[1:]...) - op, err := cmd.Output() - if err != nil { - panic(err) + var title string + if op, err := cmd.Output(); err != nil { + log.Printf("command failed, cmd: %v, err: %v", cmd, err) + title = "ERROR" + } else { + title = string(op) } d.Update(id, true, func(i *db.Item) { - i.Title = string(op) + i.Title = title }) }