Skip to content

Commit dcadf22

Browse files
committed
feat(nuxt): Add server error hook
1 parent 2e3ae56 commit dcadf22

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

packages/nuxt/src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3-
import { addPlugin, addPluginTemplate, createResolver, defineNuxtModule } from '@nuxt/kit';
3+
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
44
import type { SentryNuxtOptions } from './common/types';
55

66
export type ModuleOptions = SentryNuxtOptions;
@@ -44,6 +44,8 @@ export default defineNuxtModule<ModuleOptions>({
4444
`import "${buildDirResolver.resolve(`/${serverConfigFile}`)}"\n` +
4545
'export default defineNuxtPlugin(() => {})',
4646
});
47+
48+
addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server'));
4749
}
4850
},
4951
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { captureException } from '@sentry/node';
2+
import { H3Error } from 'h3';
3+
import { defineNitroPlugin } from 'nitropack/runtime';
4+
5+
export default defineNitroPlugin(nitroApp => {
6+
nitroApp.hooks.hook('error', (error, context) => {
7+
// Do not handle 404 and 422
8+
if (error instanceof H3Error) {
9+
if (error.statusCode === 404 || error.statusCode === 422) {
10+
return;
11+
}
12+
}
13+
14+
if (context) {
15+
captureException(error, {
16+
captureContext: { extra: { nuxt: context } },
17+
mechanism: { handled: false },
18+
});
19+
} else {
20+
captureException(error);
21+
}
22+
});
23+
});

0 commit comments

Comments
 (0)