better error handling

This commit is contained in:
AustrianToast 2024-06-03 22:15:43 +02:00
parent 9e605e34b1
commit 3db31b6ddc
No known key found for this signature in database
GPG Key ID: 5CD422268E489EB4

20
main.go
View File

@ -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")
}