Skip to content

Commit

Permalink
chore(store-indexer, store-sync): add explicit error logs (#2045)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Dec 8, 2023
1 parent 5d737cf commit 0a3b9b1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .changeset/fast-pandas-explain.md
@@ -0,0 +1,8 @@
---
"@latticexyz/store-indexer": patch
"@latticexyz/store-sync": patch
---

Added explicit error logs for unexpected situations.
Previously all `debug` logs were going to `stderr`, which made it hard to find the unexpected errors.
Now `debug` logs go to `stdout` and we can add explicit `stderr` logs.
16 changes: 8 additions & 8 deletions packages/store-indexer/src/postgres/apiRoutes.ts
Expand Up @@ -6,7 +6,7 @@ import { input } from "@latticexyz/store-sync/indexer-client";
import { storeTables } from "@latticexyz/store-sync";
import { queryLogs } from "./queryLogs";
import { recordToLog } from "./recordToLog";
import { debug } from "../debug";
import { debug, error } from "../debug";
import { createBenchmark } from "@latticexyz/common";
import { compress } from "../compress";

Expand All @@ -19,10 +19,10 @@ export function apiRoutes(database: Sql): Middleware {

try {
options = input.parse(typeof ctx.query.input === "string" ? JSON.parse(ctx.query.input) : {});
} catch (error) {
} catch (e) {
ctx.status = 400;
ctx.body = JSON.stringify(error);
debug(error);
ctx.body = JSON.stringify(e);
debug(e);
return;
}

Expand All @@ -36,17 +36,17 @@ export function apiRoutes(database: Sql): Middleware {
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}`);
error(`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) {
} catch (e) {
ctx.status = 500;
ctx.body = JSON.stringify(error);
debug(error);
ctx.body = JSON.stringify(e);
error(e);
}
});

Expand Down
11 changes: 9 additions & 2 deletions packages/store-sync/src/postgres/setupTables.ts
Expand Up @@ -6,6 +6,13 @@ import { debug as parentDebug } from "./debug";
import { pgDialect } from "./pgDialect";

const debug = parentDebug.extend("setupTables");
const error = parentDebug.extend("setupTables");

// Pipe debug output to stdout instead of stderr
debug.log = console.debug.bind(console);

// Pipe error output to stderr
error.log = console.error.bind(console);

export async function setupTables(
db: PgDatabase<any>,
Expand Down Expand Up @@ -73,8 +80,8 @@ export async function setupTables(
try {
debug(`dropping namespace ${schemaName} and all of its tables`);
await db.execute(sql.raw(pgDialect.schema.dropSchema(schemaName).ifExists().cascade().compile().sql));
} catch (error) {
debug(`failed to drop namespace ${schemaName}`, error);
} catch (e) {
error(`failed to drop namespace ${schemaName}`, e);
}
}
};
Expand Down
11 changes: 9 additions & 2 deletions packages/store-sync/src/postgres/shouldCleanDatabase.ts
Expand Up @@ -4,6 +4,13 @@ import { debug as parentDebug } from "./debug";
import { version as expectedVersion } from "./version";

const debug = parentDebug.extend("shouldCleanDatabase");
const error = parentDebug.extend("shouldCleanDatabase");

// Pipe debug output to stdout instead of stderr
debug.log = console.debug.bind(console);

// Pipe error output to stderr
error.log = console.error.bind(console);

/**
* @internal
Expand All @@ -28,8 +35,8 @@ export async function shouldCleanDatabase(db: PgDatabase<any>, expectedChainId:
}

return false;
} catch (error) {
console.error(error);
} catch (e) {
error(e);
debug("error while querying config table");
return true;
}
Expand Down

0 comments on commit 0a3b9b1

Please sign in to comment.