fix(api): classify DB errors in library paths

This commit is contained in:
2026-09-07 12:44:47 +08:00
parent 12f6e82308
commit 93638ed647
2 changed files with 23 additions and 5 deletions
+15 -3
View File
@@ -42,7 +42,11 @@ func (a *api) bookFromParam(c *gin.Context) (store.Book, bool) {
func (a *api) getLibRow(c *gin.Context, id int64) (store.Library, bool) {
l, 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 l, true
@@ -102,8 +106,16 @@ func (a *api) getBook(c *gin.Context) {
if !ok {
return
}
p, _ := a.st.GetProgress(c, uid(c), b.LibraryID, b.Path) // ErrNoRows → 零值 percent
lib, _ := a.st.GetLibrary(c, b.LibraryID)
p, e := a.st.GetProgress(c, uid(c), b.LibraryID, b.Path) // ErrNoRows → 零值 percent
if e != nil && !errors.Is(e, pgx.ErrNoRows) {
dbErr(c, e)
return
}
lib, e := a.st.GetLibrary(c, b.LibraryID)
if e != nil && !errors.Is(e, pgx.ErrNoRows) { // 库被并发删则留空 library 名,书仍可见
dbErr(c, e)
return
}
c.JSON(http.StatusOK, bookJSON(b, p.Percent, lib.Name))
}