From f61b4bc0903d09c4c71f01270012953adee50701 Mon Sep 17 00:00:00 2001 From: alvarius Date: Fri, 8 Dec 2023 18:30:58 +0100 Subject: [PATCH] feat(store-indexer): return a "not found" error when no snapshot is found for a `/api/logs` request (#2043) --- .changeset/yellow-bags-learn.md | 5 +++++ packages/store-indexer/src/postgres/apiRoutes.ts | 9 ++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 .changeset/yellow-bags-learn.md diff --git a/.changeset/yellow-bags-learn.md b/.changeset/yellow-bags-learn.md new file mode 100644 index 0000000000..585812d694 --- /dev/null +++ b/.changeset/yellow-bags-learn.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store-indexer": minor +--- + +The `/api/logs` indexer endpoint is now returning a `404` snapshot not found error when no snapshot is found for the provided filter instead of an empty `200` response. diff --git a/packages/store-indexer/src/postgres/apiRoutes.ts b/packages/store-indexer/src/postgres/apiRoutes.ts index dfaf74990e..4f877086a5 100644 --- a/packages/store-indexer/src/postgres/apiRoutes.ts +++ b/packages/store-indexer/src/postgres/apiRoutes.ts @@ -32,8 +32,15 @@ export function apiRoutes(database: Sql): Middleware { benchmark("query records"); const logs = records.map(recordToLog); benchmark("map records to logs"); - const blockNumber = records[0]?.chainBlockNumber ?? "-1"; + if (records.length === 0) { + ctx.status = 404; + ctx.body = "no logs found"; + debug(`no logs found for chainId ${options.chainId}, address ${options.address}, filters ${options.filters}`); + return; + } + + const blockNumber = records[0].chainBlockNumber; ctx.body = JSON.stringify({ blockNumber, logs }); ctx.status = 200; } catch (error) {