Skip to content

Commit

Permalink
fix(backend): error when repository doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent ee373fb commit 99447bc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
8 changes: 7 additions & 1 deletion server/backend/sqlite/error.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package sqlite

import "errors"
import (
"errors"
"fmt"
)

var (
// ErrDuplicateKey is returned when a unique constraint is violated.
ErrDuplicateKey = errors.New("record already exists")

// 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")
)
42 changes: 37 additions & 5 deletions server/backend/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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
}))
Expand All @@ -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)
}
Expand All @@ -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
})
Expand All @@ -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
}),
Expand All @@ -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
}),
Expand Down

0 comments on commit 99447bc

Please sign in to comment.