diff --git a/lib/add_book.go b/lib/add_book.go index 4b8ae4c..e0aa343 100644 --- a/lib/add_book.go +++ b/lib/add_book.go @@ -1,6 +1,8 @@ package lib import ( + "fmt" + "github.com/samber/lo" ) @@ -54,44 +56,44 @@ func AddBook(url string, options addBookOptions) (Book, error) { } if err := validateURL(NullStringFrom(url)); err != nil { - return book, err + return book, fmt.Errorf("URL validation failed while adding bookmark: %w", err) } if err := validateName(options.name); err != nil { - return book, err + return book, fmt.Errorf("name validation failed while adding bookmark: %w", err) } if err := validateDescription(options.description); err != nil { - return book, err + return book, fmt.Errorf("description validation failed while adding bookmark: %w", err) } if err := validateParentID(tx, options.parentID); err != nil { - return book, err + return book, fmt.Errorf("parent ID validation failed while adding bookmark: %w", err) } if err := validateTags(options.tags, make([]string, 0)); err != nil { - return book, err + return book, fmt.Errorf("tags validation failed while adding bookmark: %w", err) } id, err := addBookDB(tx, url, options.name.String, options.description, options.parentID) if err != nil { - return book, err + return book, fmt.Errorf("error while adding bookmark: %w", err) } existingTags, err := getTagsDB(tx, getTagsDBArgs{ tagsFilter: options.tags, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting tags while adding bookmark: %w", err) } tagsToAdd, _ := lo.Difference(options.tags, existingTags) if err = addTagsDB(tx, tagsToAdd); err != nil { - return book, err + return book, fmt.Errorf("error adding tags while adding bookmark: %w", err) } if err = linkTagsDB(tx, id, options.tags); err != nil { - return book, err + return book, fmt.Errorf("error linking tags while adding bookmark: %w", err) } books, err := getBooksDB(tx, getBooksDBArgs{ @@ -99,9 +101,9 @@ func AddBook(url string, options addBookOptions) (Book, error) { includeBooks: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting bookmarks while adding bookmark: %w", err) } - return books[0], err + return books[0], nil }) } diff --git a/lib/add_folder.go b/lib/add_folder.go index 7dbfc03..70199fe 100644 --- a/lib/add_folder.go +++ b/lib/add_folder.go @@ -1,5 +1,9 @@ package lib +import ( + "fmt" +) + // addFolderOptions are the optional arguments for AddFolder. type addFolderOptions struct { db NullString @@ -27,16 +31,16 @@ func AddFolder(name string, options addFolderOptions) (Book, error) { var book Book if err := validateName(NullStringFrom(name)); err != nil { - return book, err + return book, fmt.Errorf("name validation failed while adding folder: %w", err) } if err := validateParentID(tx, options.parentID); err != nil { - return book, err + return book, fmt.Errorf("parent ID validation failed while adding folder: %w", err) } id, err := addFolderDB(tx, name, options.parentID) if err != nil { - return book, err + return book, fmt.Errorf("error while adding folder: %w", err) } books, err := getBooksDB(tx, getBooksDBArgs{ @@ -44,9 +48,9 @@ func AddFolder(name string, options addFolderOptions) (Book, error) { includeFolders: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting folders while adding folder: %w", err) } - return books[0], err + return books[0], nil }) } diff --git a/lib/add_tags.go b/lib/add_tags.go index 7e3c203..a483213 100644 --- a/lib/add_tags.go +++ b/lib/add_tags.go @@ -1,6 +1,8 @@ package lib import ( + "fmt" + "github.com/samber/lo" ) @@ -29,7 +31,7 @@ func AddTags(id string, tags []string, options addTagsOptions) (Book, error) { includeBooks: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting tags while adding tags: %w", err) } if len(books) != 1 || books[0].IsFolder { @@ -37,7 +39,7 @@ func AddTags(id string, tags []string, options addTagsOptions) (Book, error) { } if err := validateTags(tags, books[0].Tags); err != nil { - return book, err + return book, fmt.Errorf("tags validation failed while adding tags: %w", err) } existingTags, err := getTagsDB(tx, getTagsDBArgs{ @@ -49,11 +51,11 @@ func AddTags(id string, tags []string, options addTagsOptions) (Book, error) { tagsToAdd, _ := lo.Difference(tags, existingTags) if err = addTagsDB(tx, tagsToAdd); err != nil { - return book, err + return book, fmt.Errorf("error while adding tags: %w", err) } if err = linkTagsDB(tx, id, tags); err != nil { - return book, err + return book, fmt.Errorf("error linking tags while adding tags: %w", err) } books, err = getBooksDB(tx, getBooksDBArgs{ @@ -61,7 +63,7 @@ func AddTags(id string, tags []string, options addTagsOptions) (Book, error) { includeBooks: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting bookmarks while adding tags: %w", err) } return books[0], nil diff --git a/lib/config.go b/lib/config.go index 8995ece..c8fb93b 100644 --- a/lib/config.go +++ b/lib/config.go @@ -2,6 +2,7 @@ package lib import ( "errors" + "fmt" "os" "path/filepath" "runtime" @@ -40,7 +41,7 @@ func GetConfig() (Config, error) { configPath, err := getConfigPath(runtime.GOOS, os.UserHomeDir, filepath.Join) if err != nil { - return config, err + return config, fmt.Errorf("error getting config path while getting config: %w", err) } var k = koanf.New(".") @@ -48,13 +49,13 @@ func GetConfig() (Config, error) { if strings.Contains(err.Error(), "no such file or directory") { return config, ErrConfigMissing } else { - return config, err + return config, fmt.Errorf("error loading config while getting config: %w", err) } } err = k.Unmarshal("", &config) if err != nil { - return config, err + return config, fmt.Errorf("error unmarshalling config while getting config: %w", err) } return config, nil @@ -65,18 +66,18 @@ func GetConfig() (Config, error) { func UpdateConfig(update updateConfigFn) error { config, err := GetConfig() if err != nil && !errors.Is(err, ErrConfigMissing) { - return err + return fmt.Errorf("error getting config while updating config: %w", err) } if errors.Is(err, ErrConfigMissing) { folder, err := getFolderPath(runtime.GOOS, os.UserHomeDir, filepath.Join) if err != nil { - return err + return fmt.Errorf("error getting config folder path while updating config: %w", err) } err = os.MkdirAll(folder, os.ModePerm) if err != nil { - return err + return fmt.Errorf("error making config folder while updating config: %w", err) } } @@ -85,28 +86,28 @@ func UpdateConfig(update updateConfigFn) error { var k = koanf.New(".") err = k.Load(structs.Provider(config, "koanf"), nil) if err != nil { - return err + return fmt.Errorf("error loading config while updating config: %w", err) } buffer, err := k.Marshal(toml.Parser()) if err != nil { - return err + return fmt.Errorf("error marshalling config while updating config: %w", err) } configPath, err := getConfigPath(runtime.GOOS, os.UserHomeDir, filepath.Join) if err != nil { - return err + return fmt.Errorf("error getting config path while updating config: %w", err) } handle, err := os.Create(configPath) if err != nil { - return err + return fmt.Errorf("error creating config file while updating config: %w", err) } defer handle.Close() _, err = handle.Write(buffer) if err != nil { - return err + return fmt.Errorf("error writing config file contents while updating config: %w", err) } return nil @@ -125,11 +126,11 @@ func getDatabasePath(inputPath NullString, configPath string, goos string, mkDir } else { folder, err := getFolderPath(goos, userHome, join) if err != nil { - return "", err + return "", fmt.Errorf("error getting folder path while getting database path: %w", err) } if err = mkDirAll(folder, os.ModePerm); err != nil { - return "", err + return "", fmt.Errorf("error creating folder while getting database path: %w", err) } return join(folder, databaseFilename), nil @@ -141,7 +142,7 @@ func getDatabasePath(inputPath NullString, configPath string, goos string, mkDir func getConfigPath(goos string, userHome userHomeFn, join joinFn) (string, error) { folder, err := getFolderPath(goos, userHome, join) if err != nil { - return "", err + return "", fmt.Errorf("error getting folder path while getting config path: %w", err) } return join(folder, configFilename), nil @@ -155,7 +156,7 @@ func getConfigPath(goos string, userHome userHomeFn, join joinFn) (string, error func getFolderPath(goos string, userHome userHomeFn, join joinFn) (string, error) { home, err := userHome() if err != nil { - return "", err + return "", fmt.Errorf("error getting home path while getting folder path: %w", err) } var folder string diff --git a/lib/errors.go b/lib/errors.go index 5824e99..46ab93f 100644 --- a/lib/errors.go +++ b/lib/errors.go @@ -5,8 +5,6 @@ import ( ) var ( - // ErrUnexpected is returned when an unexpected error occurs. - ErrUnexpected = errors.New("unexpected error") // ErrNoUpdate is returned when an update is requested with no updates. ErrNoUpdate = errors.New("no update") // ErrBookNotFound is returned when a target bookmark was not found. @@ -46,5 +44,5 @@ var ( // ErrQueryTooShort is returned when a provided query is too short. ErrQueryTooShort = errors.New("query too short") // ErrConfigMissing is returned when the config file is missing. - ErrConfigMissing = errors.New("config is missing") + ErrConfigMissing = errors.New("config missing") ) diff --git a/lib/list_books.go b/lib/list_books.go index dbccc9d..02e74d8 100644 --- a/lib/list_books.go +++ b/lib/list_books.go @@ -1,5 +1,9 @@ package lib +import ( + "fmt" +) + // listBooksOptions are the optional arguments for ListBooks. type listBooksOptions struct { db NullString @@ -89,22 +93,22 @@ func ListBooks(options listBooksOptions) ([]Book, error) { } if err := validateFirst(options.first); err != nil { - return books, err + return books, fmt.Errorf("first validation failed while listing bookmarks: %w", err) } if err := validateDirection(options.direction); err != nil { - return books, err + return books, fmt.Errorf("direction validation failed while listing bookmarks: %w", err) } if err := validateOrder(options.order); err != nil { - return books, err + return books, fmt.Errorf("order validation failed while listing bookmarks: %w", err) } if err := validateQuery(options.query); err != nil { - return books, err + return books, fmt.Errorf("query validation failed while listing bookmarks: %w", err) } - return getBooksDB(tx, getBooksDBArgs{ + books, err := getBooksDB(tx, getBooksDBArgs{ includeBooks: options.includeBookmarks, includeFolders: options.includeFolders, parentID: options.parentID, @@ -115,5 +119,10 @@ func ListBooks(options listBooksOptions) ([]Book, error) { direction: options.direction, first: options.first, }) + if err != nil { + return books, fmt.Errorf("error while listing bookmarks: %w", err) + } + + return books, nil }) } diff --git a/lib/list_tags.go b/lib/list_tags.go index d276f8a..dade701 100644 --- a/lib/list_tags.go +++ b/lib/list_tags.go @@ -1,5 +1,9 @@ package lib +import ( + "fmt" +) + // listTagsOptions are the optional arguments for ListTags. type listTagsOptions struct { db NullString @@ -47,22 +51,27 @@ func ListTags(options listTagsOptions) ([]string, error) { tags := make([]string, 0) if err := validateFirst(options.first); err != nil { - return tags, err + return tags, fmt.Errorf("first validation failed while listing tags: %w", err) } if err := validateDirection(options.direction); err != nil { - return tags, err + return tags, fmt.Errorf("direction validation failed while listing tags: %w", err) } if err := validateQuery(options.query); err != nil { - return tags, err + return tags, fmt.Errorf("query validation failed while listing tags: %w", err) } - return getTagsDB(tx, getTagsDBArgs{ + tags, err := getTagsDB(tx, getTagsDBArgs{ query: options.query, after: options.after, direction: options.direction, first: options.first, }) + if err != nil { + return tags, fmt.Errorf("error while listing tags: %w", err) + } + + return tags, nil }) } diff --git a/lib/remove_book.go b/lib/remove_book.go index d61f3a7..3c7a8cf 100644 --- a/lib/remove_book.go +++ b/lib/remove_book.go @@ -1,5 +1,9 @@ package lib +import ( + "fmt" +) + // removeBookOptions are the optional arguments for RemoveBook. type removeBookOptions struct { db NullString @@ -19,7 +23,7 @@ func (o *removeBookOptions) WithDB(db string) { func RemoveBook(id string, options removeBookOptions) (err error) { return execWithTransaction(options.db, connectDB, func(tx transaction) error { if err := validateBookID(tx, id); err != nil { - return err + return fmt.Errorf("bookmark ID validation failed while removing bookmark: %w", err) } books, err := getBooksDB(tx, getBooksDBArgs{ @@ -27,20 +31,20 @@ func RemoveBook(id string, options removeBookOptions) (err error) { includeBooks: true, }) if err != nil { - return err + return fmt.Errorf("error getting bookmarks while removing bookmark: %w", err) } book := books[0] if err = unlinkTagsDB(tx, book.ID, book.Tags); err != nil { - return err + return fmt.Errorf("error unlinking tags while removing bookmark: %w", err) } if err = removeBookDB(tx, book.ID); err != nil { - return err + return fmt.Errorf("error while removing bookmark: %w", err) } if err = cleanOrphanedTagsDB(tx, book.Tags); err != nil { - return err + return fmt.Errorf("error cleaning orphaned tags while removing bookmark: %w", err) } return nil diff --git a/lib/remove_folder.go b/lib/remove_folder.go index d44c7f9..55aac0d 100644 --- a/lib/remove_folder.go +++ b/lib/remove_folder.go @@ -1,6 +1,8 @@ package lib import ( + "fmt" + "github.com/samber/lo" ) @@ -23,30 +25,30 @@ func (o *removeFolderOptions) WithDB(db string) { func RemoveFolder(id string, options removeFolderOptions) error { return execWithTransaction(options.db, connectDB, func(tx transaction) error { if err := validateParentID(tx, NullStringFrom(id)); err != nil { - return err + return fmt.Errorf("parent ID validation failed while removing folder: %w", err) } bookOrFolders, err := getParentAndChildren(tx, id) if err != nil { - return err + return fmt.Errorf("error getting folder and children while removing folder: %w", err) } for _, bookOrFolder := range lo.Reverse(bookOrFolders) { if !bookOrFolder.IsFolder { if err = unlinkTagsDB(tx, bookOrFolder.ID, bookOrFolder.Tags); err != nil { - return err + return fmt.Errorf("error unlinking tags while removing folder: %w", err) } if err = removeBookDB(tx, bookOrFolder.ID); err != nil { - return err + return fmt.Errorf("error remmoving bookmark while removing folder: %w", err) } if err = cleanOrphanedTagsDB(tx, bookOrFolder.Tags); err != nil { - return err + return fmt.Errorf("error cleaning orphaned tags while removing folder: %w", err) } } else { if err = removeFolderDB(tx, bookOrFolder.ID); err != nil { - return err + return fmt.Errorf("error while removing folder: %w", err) } } } diff --git a/lib/remove_tags.go b/lib/remove_tags.go index 5744865..3c52e5e 100644 --- a/lib/remove_tags.go +++ b/lib/remove_tags.go @@ -1,6 +1,8 @@ package lib import ( + "fmt" + "github.com/samber/lo" ) @@ -29,7 +31,7 @@ func RemoveTags(id string, tags []string, options removeTagsOptions) (Book, erro includeBooks: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting bookmarks while removing tags: %w", err) } if len(books) != 1 || books[0].IsFolder { @@ -43,11 +45,11 @@ func RemoveTags(id string, tags []string, options removeTagsOptions) (Book, erro } if err = unlinkTagsDB(tx, books[0].ID, tags); err != nil { - return book, err + return book, fmt.Errorf("error unlinking tags while removing tags: %w", err) } if err = cleanOrphanedTagsDB(tx, tags); err != nil { - return book, err + return book, fmt.Errorf("error cleaning orphaned tags while removing tags: %w", err) } books, err = getBooksDB(tx, getBooksDBArgs{ @@ -55,7 +57,7 @@ func RemoveTags(id string, tags []string, options removeTagsOptions) (Book, erro includeBooks: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting bookmarks while removing tags: %w", err) } return books[0], nil diff --git a/lib/transaction.go b/lib/transaction.go index 71a6135..463c157 100644 --- a/lib/transaction.go +++ b/lib/transaction.go @@ -36,13 +36,14 @@ type queryTxFn[T any] func(transaction) (T, error) func queryWithTransaction[T any](inputPath NullString, connectFn connectDBFn, queryFn queryTxFn[T]) (val T, err error) { db, err := connectFn(inputPath) if err != nil { + err = fmt.Errorf("error connecting to database while querying with a transaction: %w", err) return } defer db.Close() tx, err := db.Begin() if err != nil { - err = fmt.Errorf("%w: %w", ErrUnexpected, err) + err = fmt.Errorf("error beginning transaction while querying with a transaction: %w", err) return } @@ -60,13 +61,17 @@ func queryWithTransaction[T any](inputPath NullString, connectFn connectDBFn, qu // all good, commit err = tx.Commit() if err != nil { - err = fmt.Errorf("%w: %w", ErrUnexpected, err) + err = fmt.Errorf("error committing transaction while querying with a transaction: %w", err) } } }() val, err = queryFn(tx) - return val, err + if err != nil { + return val, fmt.Errorf("error while querying with a transaction: %w", err) + } + + return val, nil } // QueryTxFn is a function that operates on a transaction which doesn't return results. @@ -78,13 +83,14 @@ type execTxFn func(transaction) error func execWithTransaction(inputPath NullString, connectFn connectDBFn, execFn execTxFn) (err error) { db, err := connectFn(inputPath) if err != nil { + err = fmt.Errorf("error connecting to database while executing with a transaction: %w", err) return } defer db.Close() tx, err := db.Begin() if err != nil { - err = fmt.Errorf("%w: %w", ErrUnexpected, err) + err = fmt.Errorf("error beginning transaction while executing with a transaction: %w", err) return } @@ -102,13 +108,17 @@ func execWithTransaction(inputPath NullString, connectFn connectDBFn, execFn exe // all good, commit err = tx.Commit() if err != nil { - err = fmt.Errorf("%w: %w", ErrUnexpected, err) + err = fmt.Errorf("error committing transaction while executing with a transaction: %w", err) } } }() err = execFn(tx) - return err + if err != nil { + return fmt.Errorf("error while executing with a transaction: %w", err) + } + + return nil } // queryWithDB creates a scope for for functions that operate on a database connection which return results. @@ -119,11 +129,16 @@ func queryWithDB[T any](inputPath NullString, connectFn connectDBFn, queryFn que db, err := connectFn(inputPath) if err != nil { - return val, err + return val, fmt.Errorf("error connecting to database while querying with database: %w", err) } defer db.Close() - return queryFn(db) + val, err = queryFn(db) + if err != nil { + return val, fmt.Errorf("error while querying with database: %w", err) + } + + return val, nil } // connectDBFn is a function that connects to SQLite database @@ -133,25 +148,25 @@ type connectDBFn func(inputPath NullString) (*sql.DB, error) func connectDB(inputPath NullString) (*sql.DB, error) { config, err := GetConfig() if err != nil && !errors.Is(err, ErrConfigMissing) { - return nil, err + return nil, fmt.Errorf("error getting config wile connecting to database: %w", err) } dbLocation, err := getDatabasePath(inputPath, config.DB, runtime.GOOS, os.MkdirAll, os.UserHomeDir, filepath.Join) if err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error getting database location wile connecting to database: %w", err) } db, err := sql.Open("sqlite3", dbLocation) if err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error while connecting to database: %w", err) } if err := configureDB(db); err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error configuring database wile connecting to database: %w", err) } if err := migrateDB(db); err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error applying migrations to database wile connecting to database: %w", err) } return db, nil @@ -162,11 +177,11 @@ func connectDB(inputPath NullString) (*sql.DB, error) { // - Enforce foreign keys. func configureDB(db *sql.DB) error { if _, err := db.Exec("PRAGMA journal_mode=WAL"); err != nil { - return err + return fmt.Errorf("error turning WAL mode on while configuring database: %w", err) } if _, err := db.Exec("PRAGMA foreign_keys=on"); err != nil { - return err + return fmt.Errorf("error turning foreign key checking on while configuring database: %w", err) } return nil @@ -178,11 +193,11 @@ func migrateDB(db *sql.DB) error { goose.SetLogger(goose.NopLogger()) if err := goose.SetDialect("sqlite3"); err != nil { - return err + return fmt.Errorf("error setting dialect while applying migrations: %w", err) } if err := goose.Up(db, "migrations"); err != nil { - return err + return fmt.Errorf("error while applying migrations: %w", err) } return nil @@ -194,19 +209,19 @@ func query[T any](tx transaction, q *bqb.Query) ([]T, error) { sql, args, err := q.ToSql() if err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error building query while querying: %w", err) } rows, err := tx.Query(sql, args...) if err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error while querying: %w", err) } defer rows.Close() err = scan.RowsStrict(&results, rows) if err != nil { - return nil, fmt.Errorf("%w: %w", ErrUnexpected, err) + return nil, fmt.Errorf("error building results while querying: %w", err) } return results, nil @@ -216,11 +231,11 @@ func query[T any](tx transaction, q *bqb.Query) ([]T, error) { func exec(tx transaction, q *bqb.Query) error { sql, args, err := q.ToSql() if err != nil { - return fmt.Errorf("%w: %w", ErrUnexpected, err) + return fmt.Errorf("error building query while executing: %w", err) } if _, err = tx.Exec(sql, args...); err != nil { - return fmt.Errorf("%w: %w", ErrUnexpected, err) + return fmt.Errorf("error while executing: %w", err) } return nil @@ -230,12 +245,12 @@ func exec(tx transaction, q *bqb.Query) error { func count(tx transaction, q *bqb.Query) (int, error) { sql, args, err := q.ToSql() if err != nil { - return 0, fmt.Errorf("%w: %w", ErrUnexpected, err) + return 0, fmt.Errorf("error building query while counting: %w", err) } var count = 0 if err = tx.QueryRow(sql, args...).Scan(&count); err != nil { - return 0, fmt.Errorf("%w: %w", ErrUnexpected, err) + return 0, fmt.Errorf("error while counting: %w", err) } return count, nil diff --git a/lib/update_book.go b/lib/update_book.go index d9624c3..de99e7f 100644 --- a/lib/update_book.go +++ b/lib/update_book.go @@ -1,5 +1,9 @@ package lib +import ( + "fmt" +) + // updateBookOptions are the optional arguments for UpdateBook. type updateBookOptions struct { db NullString @@ -55,7 +59,7 @@ func UpdateBook(id string, options updateBookOptions) (Book, error) { var book Book if err := validateBookID(tx, id); err != nil { - return book, err + return book, fmt.Errorf("bookmark ID validation failed while updating bookmark: %w", err) } if !options.name.Dirty && !options.url.Dirty && !options.description.Dirty && !options.parentID.Dirty { @@ -64,25 +68,25 @@ func UpdateBook(id string, options updateBookOptions) (Book, error) { if options.name.Dirty { if err := validateName(options.name); err != nil { - return book, err + return book, fmt.Errorf("name validation failed while updating bookmark: %w", err) } } if options.url.Dirty { if err := validateURL(options.url); err != nil { - return book, err + return book, fmt.Errorf("URL validation failed while updating bookmark: %w", err) } } if options.description.Dirty { if err := validateDescription(options.description); err != nil { - return book, err + return book, fmt.Errorf("description validation failed while updating bookmark: %w", err) } } if options.parentID.Dirty { if err := validateParentID(tx, options.parentID); err != nil { - return book, err + return book, fmt.Errorf("parent ID validation failed while updating bookmark: %w", err) } } @@ -92,7 +96,7 @@ func UpdateBook(id string, options updateBookOptions) (Book, error) { description: options.description, parentID: options.parentID, }); err != nil { - return book, err + return book, fmt.Errorf("error while updating bookmark: %w", err) } books, err := getBooksDB(tx, getBooksDBArgs{ @@ -100,7 +104,7 @@ func UpdateBook(id string, options updateBookOptions) (Book, error) { includeBooks: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error getting bookmarks while updating bookmark: %w", err) } return books[0], nil diff --git a/lib/update_folder.go b/lib/update_folder.go index f52245a..4a828f3 100644 --- a/lib/update_folder.go +++ b/lib/update_folder.go @@ -1,5 +1,9 @@ package lib +import ( + "fmt" +) + // updateFolderOptions are the optional arguments for UpdateFolder. type updateFolderOptions struct { db NullString @@ -38,7 +42,7 @@ func UpdateFolder(id string, options updateFolderOptions) (Book, error) { var book Book if err := validateParentID(tx, NullStringFrom(id)); err != nil { - return book, err + return book, fmt.Errorf("bookmark ID validation failed while updating folder: %w", err) } if !options.name.Dirty && !options.parentID.Dirty { @@ -47,13 +51,13 @@ func UpdateFolder(id string, options updateFolderOptions) (Book, error) { if options.name.Dirty { if err := validateName(options.name); err != nil { - return book, err + return book, fmt.Errorf("name validation failed while updating folder: %w", err) } } if options.parentID.Dirty { if err := validateParentID(tx, options.parentID); err != nil { - return book, err + return book, fmt.Errorf("parent ID validation failed while updating folder: %w", err) } } @@ -61,7 +65,7 @@ func UpdateFolder(id string, options updateFolderOptions) (Book, error) { name: options.name, parentID: options.parentID, }); err != nil { - return book, err + return book, fmt.Errorf("error while updating folder: %w", err) } books, err := getBooksDB(tx, getBooksDBArgs{ @@ -69,7 +73,7 @@ func UpdateFolder(id string, options updateFolderOptions) (Book, error) { includeFolders: true, }) if err != nil { - return book, err + return book, fmt.Errorf("error geting bookmarks while updating folder: %w", err) } return books[0], nil diff --git a/lib/validators.go b/lib/validators.go index a302f09..d08aece 100644 --- a/lib/validators.go +++ b/lib/validators.go @@ -75,9 +75,9 @@ func validateTags(tags []string, existingTags []string) error { return ErrDuplicateTag } - r, e := regexp.Compile(`^[a-zA-Z0-9\-_]*$`) - if e != nil { - return e + r, err := regexp.Compile(`^[a-zA-Z0-9\-_]*$`) + if err != nil { + return fmt.Errorf("error compiling regex while validating tags: %w", err) } for _, tag := range tags { @@ -156,7 +156,7 @@ func validateQuery(query NullString) error { func validateBookID(tx transaction, ID string) error { exists, err := bookFolderExistsDB(tx, ID, false) if err != nil { - return fmt.Errorf("%w: %w", ErrUnexpected, err) + return fmt.Errorf("error checking if bookmark exists while validating bookmark ID") } if !exists { @@ -175,7 +175,7 @@ func validateParentID(tx transaction, parentID NullString) error { exists, err := bookFolderExistsDB(tx, parentID.String, true) if err != nil { - return fmt.Errorf("%w: %w", ErrUnexpected, err) + return fmt.Errorf("error checking if folder exists while validating parent ID") } if !exists {