Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): Add server error hook #12796

Merged
merged 9 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l/m: We could arguably skip all 4xxs (also 3xxs if that can ever happen).


if (context) {
captureException(error, {
captureContext: { extra: { nuxt: context } },
s1gr1d marked this conversation as resolved.
Show resolved Hide resolved
mechanism: { handled: false },
});
} else {
captureException(error, { mechanism: { handled: false } });
}
});
});
Loading