Improve error handling

This commit is contained in:
Marco Realacci 2022-12-06 22:14:12 +01:00
parent df5b3fdda8
commit 3af4ee6c84
11 changed files with 34 additions and 35 deletions

View file

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

View file

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

View file

@ -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

View file

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

View file

@ -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

View file

@ -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

View file

@ -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