Skip to content

Commit 978953c

Browse files
committed
fix: handle an edge case where db isn't initialized during build time yet
1 parent 156f3b0 commit 978953c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

packages/nuxt/src/runtime/plugins/database.server.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@ const SENTRY_ORIGIN = 'auto.db.nuxt';
2727
* Creates a Nitro plugin that instruments the database calls.
2828
*/
2929
export default defineNitroPlugin(() => {
30-
const db = useDatabase();
30+
try {
31+
debug.log('@sentry/nuxt: Instrumenting database...');
3132

32-
debug.log('@sentry/nuxt: Instrumenting database...');
33+
const db = useDatabase();
3334

34-
instrumentDatabase(db);
35+
instrumentDatabase(db);
3536

36-
debug.log('@sentry/nuxt: Database instrumented.');
37+
debug.log('@sentry/nuxt: Database instrumented.');
38+
} catch (error) {
39+
// During build time, we can't use the useDatabase function, so we just log an error.
40+
if (error instanceof Error && /Cannot access 'instances'/.test(error.message)) {
41+
debug.log('@sentry/nuxt: Database instrumentation skipped during build time.');
42+
return;
43+
}
44+
45+
debug.error('@sentry/nuxt: Failed to instrument database:', error);
46+
}
3747
});
3848

3949
function instrumentDatabase(db: Database): void {

0 commit comments

Comments
 (0)