refactor errors
This commit is contained in:
@@ -2,7 +2,6 @@ package core
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
_ "github.com/mattn/go-sqlite3" // Import the SQLite3 driver
|
||||
@@ -125,7 +124,10 @@ func (d *SqliteDB) Renew(id CustomerId) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
setXor, attrXor := customer.RenewKeys()
|
||||
setXor, attrXor, err := customer.RenewKeys()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
renewArgs := []any{util.Uint64ArrToByteArr(customer.Attributes.AttrVals), util.Uint64ArrToByteArr(customer.Attributes.SetVals), uuid.UUID(customer.Id).String()}
|
||||
// TODO: replace with tx
|
||||
renewQuery := `
|
||||
@@ -208,9 +210,13 @@ func (d *SqliteDB) GetCustomer(id CustomerId) (*Customer, error) {
|
||||
}()
|
||||
selectCustomer := `SELECT max_nkode_len, min_nkode_len, distinct_sets, distinct_attributes, lock_out, expiration, attribute_values, set_values FROM customer WHERE id = ?`
|
||||
rows, err := tx.Query(selectCustomer, uuid.UUID(id))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !rows.Next() {
|
||||
return nil, errors.New(fmt.Sprintf("no new row for customer %s with err %s", id, rows.Err()))
|
||||
log.Printf("no new row for customer %s with err %s", id, rows.Err())
|
||||
return nil, ErrCustomerDne
|
||||
}
|
||||
|
||||
var maxNKodeLen int
|
||||
@@ -225,10 +231,6 @@ func (d *SqliteDB) GetCustomer(id CustomerId) (*Customer, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if rows.Next() {
|
||||
return nil, errors.New(fmt.Sprintf("too many rows for customer %s", id))
|
||||
}
|
||||
customer := Customer{
|
||||
Id: id,
|
||||
NKodePolicy: NKodePolicy{
|
||||
@@ -241,9 +243,8 @@ func (d *SqliteDB) GetCustomer(id CustomerId) (*Customer, error) {
|
||||
},
|
||||
Attributes: NewCustomerAttributesFromBytes(attributeValues, setValues),
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read customer won't commit %w", err)
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &customer, nil
|
||||
}
|
||||
@@ -278,9 +279,6 @@ WHERE user.username = ? AND user.customer_id = ?
|
||||
var svgIdInterface []byte
|
||||
|
||||
err = rows.Scan(&id, &renewVal, &refreshToken, &code, &mask, &attrsPerKey, &numbOfKeys, &alphaKey, &setKey, &passKey, &maskKey, &salt, &maxNKodeLen, &idxInterface, &svgIdInterface)
|
||||
if rows.Next() {
|
||||
return nil, errors.New(fmt.Sprintf("too many rows for user %s of customer %s", username, customerId))
|
||||
}
|
||||
|
||||
userId, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
@@ -324,8 +322,7 @@ WHERE user.username = ? AND user.customer_id = ?
|
||||
}
|
||||
user.Interface.Kp = &user.Kp
|
||||
user.CipherKeys.Kp = &user.Kp
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &user, nil
|
||||
@@ -360,15 +357,14 @@ func (d *SqliteDB) getSvgsById(ids []int) ([]string, error) {
|
||||
return nil, err
|
||||
}
|
||||
if !rows.Next() {
|
||||
return nil, errors.New(fmt.Sprintf("id not found: %d", id))
|
||||
log.Printf("id not found: %d", id)
|
||||
return nil, ErrSvgDne
|
||||
}
|
||||
err = rows.Scan(&svgs[idx])
|
||||
if err != nil {
|
||||
if err = rows.Scan(&svgs[idx]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return svgs, nil
|
||||
@@ -383,16 +379,14 @@ func (d *SqliteDB) writeToDb(query string, args []any) error {
|
||||
if err != nil {
|
||||
err = tx.Rollback()
|
||||
if err != nil {
|
||||
log.Fatal(fmt.Sprintf("Write won't roll back %+v", err))
|
||||
log.Fatalf("fatal error: write won't roll back %+v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
_, err = tx.Exec(query, args...)
|
||||
if err != nil {
|
||||
if _, err = tx.Exec(query, args...); err != nil {
|
||||
return err
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
if err = tx.Commit(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -400,7 +394,7 @@ func (d *SqliteDB) writeToDb(query string, args []any) error {
|
||||
|
||||
func (d *SqliteDB) addWriteTx(query string, args []any) error {
|
||||
if d.stop {
|
||||
return errors.New("stopping database")
|
||||
return ErrStoppingDatabase
|
||||
}
|
||||
errChan := make(chan error)
|
||||
writeTx := WriteTx{
|
||||
@@ -416,31 +410,37 @@ func (d *SqliteDB) addWriteTx(query string, args []any) error {
|
||||
func (d *SqliteDB) getRandomIds(count int) ([]int, error) {
|
||||
tx, err := d.db.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
log.Print(err)
|
||||
return nil, ErrSqliteTx
|
||||
}
|
||||
rows, err := tx.Query("SELECT COUNT(*) as count FROM svg_icon;")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
log.Print(err)
|
||||
return nil, ErrSqliteTx
|
||||
}
|
||||
var tableLen int
|
||||
if !rows.Next() {
|
||||
return nil, errors.New("empty svg_icon table")
|
||||
return nil, ErrEmptySvgTable
|
||||
}
|
||||
|
||||
if err = rows.Scan(&tableLen); err != nil {
|
||||
return nil, err
|
||||
log.Print(err)
|
||||
return nil, ErrSqliteTx
|
||||
}
|
||||
|
||||
perm, err := util.RandomPermutation(tableLen)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for idx := range perm {
|
||||
perm[idx] += 1
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
log.Print(err)
|
||||
return nil, ErrSqliteTx
|
||||
}
|
||||
|
||||
return perm[:count], nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user