Skip to content

Commit

Permalink
feat(nuxt): Add server error hook
Browse files Browse the repository at this point in the history
  • Loading branch information
s1gr1d committed Jul 8, 2024
1 parent ecc95e7 commit 48c34df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fs from 'fs';
import * as path from 'path';
import { addPlugin, addPluginTemplate, createResolver, defineNuxtModule } from '@nuxt/kit';
import { addPlugin, addPluginTemplate, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit';
import type { SentryNuxtOptions } from './common/types';

export type ModuleOptions = SentryNuxtOptions;
Expand Down Expand Up @@ -44,6 +44,8 @@ export default defineNuxtModule<ModuleOptions>({
`import "${buildDirResolver.resolve(`/${serverConfigFile}`)}"\n` +
'export default defineNuxtPlugin(() => {})',
});

addServerPlugin(moduleDirResolver.resolve('./runtime/plugins/sentry.server'));
}
},
});
Expand Down
23 changes: 23 additions & 0 deletions packages/nuxt/src/runtime/plugins/sentry.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { captureException } from '@sentry/node';
import { H3Error } from 'h3';
import { defineNitroPlugin } from 'nitropack/runtime';

export default defineNitroPlugin(nitroApp => {
nitroApp.hooks.hook('error', (error, context) => {
// Do not handle 404 and 422
if (error instanceof H3Error) {
if (error.statusCode === 404 || error.statusCode === 422) {
return;
}
}

if (context) {
captureException(error, {
captureContext: { extra: { nuxt: context } },
mechanism: { handled: false },
});
} else {
captureException(error);
}
});
});

0 comments on commit 48c34df

Please sign in to comment.