From 3af4ee6c84c8b8e55c3d0683f041125c1d5f3b09 Mon Sep 17 00:00:00 2001 From: Marco Realacci Date: Tue, 6 Dec 2022 22:14:12 +0100 Subject: [PATCH] Improve error handling --- service/api/api-handler.go | 3 --- service/api/get-context-reply.go | 14 -------------- service/api/get-hello-world.go | 12 ------------ service/api/photos.go | 11 ++++++++--- service/database/database.go | 2 +- service/database/db-comments.go | 2 ++ service/database/db-likes.go | 3 +++ service/database/db-photos.go | 15 +++++++++++++-- service/database/db-profile.go | 2 ++ service/database/db-stream.go | 2 ++ service/database/db-users.go | 3 +++ 11 files changed, 34 insertions(+), 35 deletions(-) delete mode 100644 service/api/get-context-reply.go delete mode 100644 service/api/get-hello-world.go diff --git a/service/api/api-handler.go b/service/api/api-handler.go index bc81e3e..1b50cca 100644 --- a/service/api/api-handler.go +++ b/service/api/api-handler.go @@ -39,9 +39,6 @@ func (rt *_router) Handler() http.Handler { rt.router.GET("/stream", rt.wrap(rt.GetUserStream)) - rt.router.GET("/", rt.getHelloWorld) - rt.router.GET("/context", rt.wrap(rt.getContextReply)) - // Special routes rt.router.GET("/liveness", rt.liveness) diff --git a/service/api/get-context-reply.go b/service/api/get-context-reply.go deleted file mode 100644 index fcf9656..0000000 --- a/service/api/get-context-reply.go +++ /dev/null @@ -1,14 +0,0 @@ -package api - -import ( - "github.com/notherealmarco/WASAPhoto/service/api/reqcontext" - "github.com/julienschmidt/httprouter" - "net/http" -) - -// getContextReply is an example of HTTP endpoint that returns "Hello World!" as a plain text. The signature of this -// handler accepts a reqcontext.RequestContext (see httpRouterHandler). -func (rt *_router) getContextReply(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) { - w.Header().Set("content-type", "text/plain") - _, _ = w.Write([]byte("Hello World!")) -} diff --git a/service/api/get-hello-world.go b/service/api/get-hello-world.go deleted file mode 100644 index c0ec9ed..0000000 --- a/service/api/get-hello-world.go +++ /dev/null @@ -1,12 +0,0 @@ -package api - -import ( - "github.com/julienschmidt/httprouter" - "net/http" -) - -// getHelloWorld is an example of HTTP endpoint that returns "Hello world!" as a plain text -func (rt *_router) getHelloWorld(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - w.Header().Set("content-type", "text/plain") - _, _ = w.Write([]byte("Hello World!")) -} diff --git a/service/api/photos.go b/service/api/photos.go index 3c51297..98e5ddd 100644 --- a/service/api/photos.go +++ b/service/api/photos.go @@ -15,7 +15,7 @@ import ( func (rt *_router) PostPhoto(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) { - //defer r.Body.Close() + // defer r.Body.Close() uid := ps.ByName("user_id") @@ -109,7 +109,12 @@ func (rt *_router) GetPhoto(w http.ResponseWriter, r *http.Request, ps httproute defer file.Close() - io.Copy(w, file) + _, err = io.Copy(w, file) + + if err != nil { + helpers.SendInternalError(err, "Error writing response", w, rt.baseLogger) + return + } } func (rt *_router) DeletePhoto(w http.ResponseWriter, r *http.Request, ps httprouter.Params, ctx reqcontext.RequestContext) { @@ -134,7 +139,7 @@ func (rt *_router) DeletePhoto(w http.ResponseWriter, r *http.Request, ps httpro if err != nil { helpers.SendInternalError(err, "Error deleting photo from database", w, rt.baseLogger) return - } //todo: maybe let's use a transaction also here + } // todo: maybe let's use a transaction also here if !deleted { helpers.SendNotFound(w, "Photo not found", rt.baseLogger) diff --git a/service/database/database.go b/service/database/database.go index 087f063..efaf06d 100644 --- a/service/database/database.go +++ b/service/database/database.go @@ -47,7 +47,7 @@ type AppDatabase interface { SearchByName(name string, requesting_uid string, start_index int, limit int) (*[]structures.UIDName, error) - UpdateUsername(uid, name string) (QueryResult, error) + UpdateUsername(uid string, name string) (QueryResult, error) GetUserFollowers(uid string, requesting_uid string, start_index int, limit int) (QueryResult, *[]structures.UIDName, error) GetUserFollowing(uid string, requesting_uid string, start_index int, offset int) (QueryResult, *[]structures.UIDName, error) diff --git a/service/database/db-comments.go b/service/database/db-comments.go index 4247b7e..96b2adb 100644 --- a/service/database/db-comments.go +++ b/service/database/db-comments.go @@ -114,6 +114,8 @@ func (db *appdbimpl) GetComments(uid string, photo_id int64, requesting_uid stri comments := make([]structures.Comment, 0) + defer rows.Close() + for rows.Next() { var c structures.Comment err = rows.Scan(&c.CommentID, &c.UID, &c.Comment, &c.Date, &c.Name) diff --git a/service/database/db-likes.go b/service/database/db-likes.go index d56409a..2c763ff 100644 --- a/service/database/db-likes.go +++ b/service/database/db-likes.go @@ -36,6 +36,9 @@ func (db *appdbimpl) GetPhotoLikes(uid string, photo int64, requesting_uid strin } likes := make([]structures.UIDName, 0) + + defer rows.Close() + for rows.Next() { var uid string var name string diff --git a/service/database/db-photos.go b/service/database/db-photos.go index 95f857f..ae3dfea 100644 --- a/service/database/db-photos.go +++ b/service/database/db-photos.go @@ -1,6 +1,7 @@ package database import ( + "fmt" "time" ) @@ -14,13 +15,23 @@ func (db *appdbimpl) PostPhoto(uid string) (DBTransaction, int64, error) { res, err := tx.Exec(`INSERT INTO "photos" ("user", "date") VALUES (?, ?)`, uid, time.Now().Format(time.RFC3339)) if err != nil { - tx.Rollback() // error ? + err_rb := tx.Rollback() + // If rollback fails, we return the original error plus the rollback error + if err_rb != nil { + err = fmt.Errorf("%w; %w", err, err_rb) + } + return nil, 0, err } id, err := res.LastInsertId() if err != nil { - tx.Rollback() // error ? + err_rb := tx.Rollback() + // If rollback fails, we return the original error plus the rollback error + if err_rb != nil { + err = fmt.Errorf("%w; %w", err, err_rb) + } + return nil, 0, err } diff --git a/service/database/db-profile.go b/service/database/db-profile.go index 21699fa..3b3c8b4 100644 --- a/service/database/db-profile.go +++ b/service/database/db-profile.go @@ -93,6 +93,8 @@ func (db *appdbimpl) GetUserPhotos(uid string, requesting_uid string, start_inde photos := make([]structures.UserPhoto, 0) + defer rows.Close() + for rows.Next() { // If there is a next row, we create an instance of Photo and add it to the slice var photo structures.UserPhoto diff --git a/service/database/db-stream.go b/service/database/db-stream.go index c5542e9..644b5b9 100644 --- a/service/database/db-stream.go +++ b/service/database/db-stream.go @@ -34,6 +34,8 @@ func (db *appdbimpl) GetUserStream(uid string, start_index int, limit int) (*[]s photos := make([]structures.Photo, 0) + defer rows.Close() + for rows.Next() { // If there is a next row, we create an instance of Photo and add it to the slice var photo structures.Photo diff --git a/service/database/db-users.go b/service/database/db-users.go index 8912816..b711cd2 100644 --- a/service/database/db-users.go +++ b/service/database/db-users.go @@ -150,6 +150,9 @@ func (db *appdbimpl) uidNameQuery(rows *sql.Rows, err error) (*[]structures.UIDN } var followers []structures.UIDName = make([]structures.UIDName, 0) + + defer rows.Close() + for rows.Next() { var uid string var name string