From 3db31b6ddcd518f10aa508b5bdacfd77b2299071 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Mon, 3 Jun 2024 22:15:43 +0200 Subject: [PATCH] better error handling --- main.go | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index e4f9574..c9d4c8d 100644 --- a/main.go +++ b/main.go @@ -109,15 +109,15 @@ func receiveChunk(c *gin.Context) { f, err := os.OpenFile(fmt.Sprintf("%s/%s", uploadFolder, c.GetHeader("file-name")), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil { c.JSON(http.StatusInternalServerError, "") - log.Fatal(err) + log.Panicf("receiveChunk: %v\n", err) } if _, err := f.Write(chunk); err != nil { c.JSON(http.StatusInternalServerError, "") - log.Fatal(err) + log.Panicf("receiveChunk: %v\n", err) } if err := f.Close(); err != nil { c.JSON(http.StatusInternalServerError, "") - log.Fatal(err) + log.Panicf("receiveChunk: %v\n", err) } c.JSON(http.StatusOK, "Received chunk") @@ -136,9 +136,7 @@ func finishUpload(c *gin.Context) { log.Panicf("finishUpload: %v\n", err) } - allVideos := gin.H{} - - c.JSON(http.StatusOK, allVideos) + c.JSON(http.StatusOK, "File uploaded successfully") } func listVideos(c *gin.Context) { @@ -186,7 +184,7 @@ func getVideo(c *gin.Context) { log.Panicf("getVideo: %v\n", err) } - c.JSON(http.StatusOK, gin.H{}) + c.JSON(http.StatusOK, filepath) } func deleteVideo(c *gin.Context) { @@ -200,16 +198,14 @@ func deleteVideo(c *gin.Context) { var filepath string err = rows.Scan(&filepath) if err != nil { - log.Panicf("deleteVideo: %v\n", err) c.JSON(http.StatusBadRequest, "Video does not exist") - return + log.Panicf("deleteVideo: %v\n", err) } fmt.Println(filepath) err = rows.Err() if err != nil { + c.JSON(http.StatusInternalServerError, "") log.Panicf("deleteVideo: %v\n", err) - c.JSON(http.StatusInternalServerError, "Scanning the row didn't work") - return } _, err = dbpool.Exec(context.Background(), "delete from videos where id = $1", inputId) @@ -223,5 +219,5 @@ func deleteVideo(c *gin.Context) { log.Panicf("deleteVideo: %v\n", err) } - c.JSON(http.StatusOK, gin.H{}) // return list of videos + c.JSON(http.StatusOK, "File deleted successfully") }