From 2d9cb73b789ed8cdd79122a8b37b276b096c1de4 Mon Sep 17 00:00:00 2001 From: livepeer-tessa Date: Mon, 30 Mar 2026 06:24:51 +0000 Subject: [PATCH] fix: suppress spurious 'api_token id=undefined not found' error on lastSeen flush MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When table.update() is called with an array of SQL conditions (instead of a plain id string), the error path in table.ts was constructing the NotFoundError message using doc.id — but doc is the partial update payload {lastSeen: ...} which has no id field, producing the misleading message: api_token id=undefined not found Two fixes: 1. tracking.ts: pass throwIfEmpty:false for lastSeen updates. These are best-effort — the token may have been deleted between the auth check and the 60s-deferred flush, so a no-op UPDATE is not an error and shouldn't throw at all. This is the primary fix. 2. table.ts: when query is an array, use the id from the query context (or '(unknown)') rather than doc.id, which is never set in this call path. This prevents the misleading 'id=undefined' in any future error messages from other callers of update() with array queries. Fixes #761 in daydreamlive/scope (18 occurrences of this error on staging, hourly for token 2f25c979-904f-4fce-b329-4174b54fdcbf). Signed-off-by: livepeer-tessa --- packages/api/src/middleware/tracking.ts | 3 +++ packages/api/src/store/table.ts | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/api/src/middleware/tracking.ts b/packages/api/src/middleware/tracking.ts index 1e671e8cf..405a4527a 100644 --- a/packages/api/src/middleware/tracking.ts +++ b/packages/api/src/middleware/tracking.ts @@ -52,6 +52,9 @@ class Tracker { sql`coalesce((data->'lastSeen')::bigint, 0) < ${lastSeen}`, ], { lastSeen }, + // lastSeen is best-effort: the record may have been deleted since the + // token was last seen, so a no-op update is not an error. + { throwIfEmpty: false }, ); } catch (err) { console.log( diff --git a/packages/api/src/store/table.ts b/packages/api/src/store/table.ts index 2806945bd..0be206120 100644 --- a/packages/api/src/store/table.ts +++ b/packages/api/src/store/table.ts @@ -251,7 +251,11 @@ export default class Table { } if (res.rowCount < 1 && throwIfEmpty) { - throw new NotFoundError(`${this.name} id=${doc.id} not found`); + // When query is an array of SQL conditions, doc may not contain an id. + // Fall back to a generic message to avoid misleading "id=undefined" errors. + const idHint = + typeof query === "string" ? query : (doc as any).id ?? "(unknown)"; + throw new NotFoundError(`${this.name} id=${idHint} not found`); } return res; }