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:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"bookhoard/internal/database"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -98,7 +99,7 @@ func (h *AnalyticsHandler) GetReadingStats(c *echo.Context) error {
|
||||
CreatedAt_2: pgtype.Timestamptz{Time: endTime, Valid: true},
|
||||
})
|
||||
|
||||
if err != nil && err != pgx.ErrNoRows {
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get reading history")
|
||||
}
|
||||
|
||||
@@ -211,7 +212,7 @@ func (h *AnalyticsHandler) GetDeviceUsage(c *echo.Context) error {
|
||||
ctx := context.Background()
|
||||
|
||||
usage, err := h.db.GetUserDeviceUsage(ctx, user.ID)
|
||||
if err != nil && err != pgx.ErrNoRows {
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get device usage")
|
||||
}
|
||||
|
||||
@@ -263,7 +264,7 @@ func (h *AnalyticsHandler) GetPopularBooks(c *echo.Context) error {
|
||||
Limit: limitInt,
|
||||
})
|
||||
|
||||
if err != nil && err != pgx.ErrNoRows {
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get popular books")
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user