refactor: use errors.Is()/errors.AsType() for error comparison and rename shadowed variables

Replace direct error equality checks (err == pgx.ErrNoRows, err != http.ErrServerClosed)
with the idiomatic errors.Is() function throughout handlers, services, middleware, and app
startup. This correctly handles wrapped error chains.

Also replace a raw type assertion (*HTTPError) with errors.AsType[*HTTPError]() in the
error handler middleware for consistency.

Additionally, rename shadowed variables for clarity:
- sidecar.go: config -> sidecarConfig, systemConfig (shadowed package-level vars)
- media_scanner.go: uuid -> uuidString (shadowed the uuid package import)
This commit is contained in:
2026-04-20 21:20:22 -04:00
parent d8d6334052
commit 9ccff320a1
15 changed files with 59 additions and 45 deletions
+9 -8
View File
@@ -15,6 +15,7 @@ import (
"encoding/hex"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"image"
_ "image/jpeg"
@@ -590,7 +591,7 @@ func (s *MediaScanner) processMediaFile(ctx context.Context, path string) (bool,
fmt.Printf("Media item already exists with same size, skipping: %s\n", path)
return false, nil
}
} else if err != pgx.ErrNoRows {
} else if !errors.Is(err, pgx.ErrNoRows) {
fmt.Printf("Database error checking media item existence: %v\n", err)
return false, fmt.Errorf("failed to check if media item exists: %v", err)
}
@@ -2605,7 +2606,7 @@ func (s *MediaScanner) scanDirectory(ctx context.Context, dirPath string) {
LibraryID: libraryID,
})
if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
if _, err := s.processMediaFile(ctx, path); err != nil {
s.errors++
} else {
@@ -2780,7 +2781,7 @@ func (s *MediaScanner) SyncFilesystemWithDatabase(ctx context.Context) error {
FilePath: relPath,
LibraryID: libraryID,
})
if err == pgx.ErrNoRows {
if errors.Is(err, pgx.ErrNoRows) {
// New file found - scan it
absPath := folder + "/" + relPath
if _, err := os.Stat(absPath); err == nil {
@@ -2848,19 +2849,19 @@ func (s *MediaScanner) extractOPFIdentifiers(epubPath string) (opfIdentifier, op
return "", "", "low", nil
}
var identifier, uuid string
var identifier, uuidString string
for _, id := range identifiers {
id = strings.TrimSpace(id)
// Check for UUID format (urn:uuid:)
if trimmed, found := strings.CutPrefix(strings.ToLower(id), "urn:uuid:"); found {
uuid = trimmed
uuidString = trimmed
continue
}
// Check if it's a plain UUID (8-4-4-4-12 format)
if isValidUUID(id) {
uuid = id
uuidString = id
continue
}
@@ -2879,9 +2880,9 @@ func (s *MediaScanner) extractOPFIdentifiers(epubPath string) (opfIdentifier, op
}
}
confidence = s.determineHashConfidence(uuid, identifier)
confidence = s.determineHashConfidence(uuidString, identifier)
return identifier, uuid, confidence, nil
return identifier, uuidString, confidence, nil
}
// isValidUUID checks if string is a valid UUID (8-4-4-4-12 format)
+2 -1
View File
@@ -3,6 +3,7 @@ package services
import (
"bookhoard/internal/database"
"context"
"errors"
"strings"
"github.com/jackc/pgx/v5"
@@ -214,7 +215,7 @@ func (s *SearchService) SearchFieldValues(ctx context.Context, params FieldSearc
// Shared between JSON endpoint and HTML rendering
func (s *SearchService) ExecuteSearch(ctx context.Context, params SearchParams) ([]database.SearchMediaItemsUnifiedRow, int, error) {
results, err := s.SearchMediaItemsUnified(ctx, params)
if err != nil && err != pgx.ErrNoRows {
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
return nil, 0, err
}