mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-03-14 06:06:15 +01:00
Improve error handling
This commit is contained in:
parent
df5b3fdda8
commit
3af4ee6c84
11 changed files with 34 additions and 35 deletions
|
@ -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)
|
||||
|
||||
|
|
|
@ -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!"))
|
||||
}
|
|
@ -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!"))
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue