From 99447bc8d4c2a8b384093553eb0bc8c460ff689d Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Wed, 26 Apr 2023 13:25:00 -0400 Subject: [PATCH] fix(backend): error when repository doesn't exist --- server/backend/sqlite/error.go | 8 ++++++- server/backend/sqlite/sqlite.go | 42 +++++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/server/backend/sqlite/error.go b/server/backend/sqlite/error.go index 72020de5b..1422697a9 100644 --- a/server/backend/sqlite/error.go +++ b/server/backend/sqlite/error.go @@ -1,6 +1,9 @@ package sqlite -import "errors" +import ( + "errors" + "fmt" +) var ( // ErrDuplicateKey is returned when a unique constraint is violated. @@ -8,4 +11,7 @@ var ( // ErrNoRecord is returned when a record is not found. ErrNoRecord = errors.New("record not found") + + // ErrRepoNotExist is returned when a repository does not exist. + ErrRepoNotExist = fmt.Errorf("repository does not exist") ) diff --git a/server/backend/sqlite/sqlite.go b/server/backend/sqlite/sqlite.go index eb480cf24..bc69e2628 100644 --- a/server/backend/sqlite/sqlite.go +++ b/server/backend/sqlite/sqlite.go @@ -313,7 +313,7 @@ func (d *SqliteBackend) Repository(repo string) (backend.Repository, error) { if count == 0 { logger.Warn("repository exists but not found in database", "repo", repo) - return nil, fmt.Errorf("repository does not exist") + return nil, ErrRepoNotExist } return &Repo{ @@ -330,7 +330,8 @@ func (d *SqliteBackend) Description(repo string) (string, error) { repo = utils.SanitizeRepo(repo) var desc string if err := wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { - return tx.Get(&desc, "SELECT description FROM repo WHERE name = ?", repo) + row := tx.QueryRow("SELECT description FROM repo WHERE name = ?", repo) + return row.Scan(&desc) }); err != nil { return "", wrapDbErr(err) } @@ -360,7 +361,8 @@ func (d *SqliteBackend) IsPrivate(repo string) (bool, error) { repo = utils.SanitizeRepo(repo) var private bool if err := wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { - return tx.Get(&private, "SELECT private FROM repo WHERE name = ?", repo) + row := tx.QueryRow("SELECT private FROM repo WHERE name = ?", repo) + return row.Scan(&private) }); err != nil { return false, wrapDbErr(err) } @@ -375,7 +377,8 @@ func (d *SqliteBackend) IsHidden(repo string) (bool, error) { repo = utils.SanitizeRepo(repo) var hidden bool if err := wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { - return tx.Get(&hidden, "SELECT hidden FROM repo WHERE name = ?", repo) + row := tx.QueryRow("SELECT hidden FROM repo WHERE name = ?", repo) + return row.Scan(&hidden) }); err != nil { return false, wrapDbErr(err) } @@ -389,6 +392,13 @@ func (d *SqliteBackend) IsHidden(repo string) (bool, error) { func (d *SqliteBackend) SetHidden(repo string, hidden bool) error { repo = utils.SanitizeRepo(repo) return wrapDbErr(wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { + var count int + if err := tx.Get(&count, "SELECT COUNT(*) FROM repo WHERE name = ?", repo); err != nil { + return err + } + if count == 0 { + return ErrRepoNotExist + } _, err := tx.Exec("UPDATE repo SET hidden = ?, updated_at = CURRENT_TIMESTAMP WHERE name = ?;", hidden, repo) return err })) @@ -401,7 +411,8 @@ func (d *SqliteBackend) ProjectName(repo string) (string, error) { repo = utils.SanitizeRepo(repo) var name string if err := wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { - return tx.Get(&name, "SELECT project_name FROM repo WHERE name = ?", repo) + row := tx.QueryRow("SELECT project_name FROM repo WHERE name = ?", repo) + return row.Scan(&name) }); err != nil { return "", wrapDbErr(err) } @@ -415,6 +426,13 @@ func (d *SqliteBackend) ProjectName(repo string) (string, error) { func (d *SqliteBackend) SetDescription(repo string, desc string) error { repo = utils.SanitizeRepo(repo) return wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { + var count int + if err := tx.Get(&count, "SELECT COUNT(*) FROM repo WHERE name = ?", repo); err != nil { + return err + } + if count == 0 { + return ErrRepoNotExist + } _, err := tx.Exec("UPDATE repo SET description = ? WHERE name = ?", desc, repo) return err }) @@ -427,6 +445,13 @@ func (d *SqliteBackend) SetPrivate(repo string, private bool) error { repo = utils.SanitizeRepo(repo) return wrapDbErr( wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { + var count int + if err := tx.Get(&count, "SELECT COUNT(*) FROM repo WHERE name = ?", repo); err != nil { + return err + } + if count == 0 { + return ErrRepoNotExist + } _, err := tx.Exec("UPDATE repo SET private = ? WHERE name = ?", private, repo) return err }), @@ -440,6 +465,13 @@ func (d *SqliteBackend) SetProjectName(repo string, name string) error { repo = utils.SanitizeRepo(repo) return wrapDbErr( wrapTx(d.db, d.ctx, func(tx *sqlx.Tx) error { + var count int + if err := tx.Get(&count, "SELECT COUNT(*) FROM repo WHERE name = ?", repo); err != nil { + return err + } + if count == 0 { + return ErrRepoNotExist + } _, err := tx.Exec("UPDATE repo SET project_name = ? WHERE name = ?", name, repo) return err }),