fix(api): classify DB errors in library paths
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"booklib/internal/bookfile"
|
||||
"booklib/internal/store"
|
||||
@@ -60,7 +62,7 @@ func (a *api) createLibrary(c *gin.Context) {
|
||||
err(c, http.StatusConflict, "exists", "root_path taken")
|
||||
return
|
||||
}
|
||||
err(c, http.StatusBadRequest, "bad_request", "invalid input")
|
||||
dbErr(c, e)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"id": id, "name": req.Name, "root_path": filepath.Clean(req.RootPath)})
|
||||
@@ -74,7 +76,11 @@ func (a *api) getLibrary(c *gin.Context) (store.Library, bool) {
|
||||
}
|
||||
lib, e := a.st.GetLibrary(c, id)
|
||||
if e != nil {
|
||||
err(c, http.StatusNotFound, "not_found", "no such library")
|
||||
if errors.Is(e, pgx.ErrNoRows) {
|
||||
err(c, http.StatusNotFound, "not_found", "no such library")
|
||||
return store.Library{}, false
|
||||
}
|
||||
dbErr(c, e)
|
||||
return store.Library{}, false
|
||||
}
|
||||
return lib, true
|
||||
|
||||
Reference in New Issue
Block a user