Skip to content

Commit 8d17ffc

Browse files
authored
feat(nextjs): Expose top level buildTime errorHandler option (#16718)
Exposes a `errorHandler` option in `withSentryConfig` for handling built-time errors.
1 parent e0c9059 commit 8d17ffc

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

packages/nextjs/src/config/types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,23 @@ export type SentryBuildOptions = {
445445
*/
446446
automaticVercelMonitors?: boolean;
447447

448+
/**
449+
* When an error occurs during release creation or sourcemaps upload, the plugin will call this function.
450+
*
451+
* By default, the plugin will simply throw an error, thereby stopping the bundling process.
452+
* If an `errorHandler` callback is provided, compilation will continue, unless an error is
453+
* thrown in the provided callback.
454+
*
455+
* To allow compilation to continue but still emit a warning, set this option to the following:
456+
*
457+
* ```js
458+
* (err) => {
459+
* console.warn(err);
460+
* }
461+
* ```
462+
*/
463+
errorHandler?: (err: Error) => void;
464+
448465
/**
449466
* Contains a set of experimental flags that might change in future releases. These flags enable
450467
* features that are still in development and may be modified, renamed, or removed without notice.

packages/nextjs/src/config/webpackPluginOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export function getWebpackPluginOptions(
6262
project: sentryBuildOptions.project,
6363
telemetry: sentryBuildOptions.telemetry,
6464
debug: sentryBuildOptions.debug,
65+
errorHandler: sentryBuildOptions.errorHandler,
6566
reactComponentAnnotation: {
6667
...sentryBuildOptions.reactComponentAnnotation,
6768
...sentryBuildOptions.unstable_sentryWebpackPluginOptions?.reactComponentAnnotation,

packages/nextjs/test/config/webpack/webpackPluginOptions.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,23 @@ describe('getWebpackPluginOptions()', () => {
138138
});
139139
});
140140

141+
it('forwards errorHandler option', () => {
142+
const buildContext = generateBuildContext({ isServer: false });
143+
const mockErrorHandler = (err: Error) => {
144+
throw err;
145+
};
146+
147+
const generatedPluginOptions = getWebpackPluginOptions(
148+
buildContext,
149+
{
150+
errorHandler: mockErrorHandler,
151+
},
152+
undefined,
153+
);
154+
155+
expect(generatedPluginOptions.errorHandler).toBe(mockErrorHandler);
156+
});
157+
141158
it('returns the right `assets` and `ignore` values during the server build', () => {
142159
const buildContext = generateBuildContext({ isServer: true });
143160
const generatedPluginOptions = getWebpackPluginOptions(buildContext, {}, undefined);

0 commit comments

Comments
 (0)