Skip to content

Commit 303e3c0

Browse files
authored
Merge pull request #150 from CivicDataLab/149-integrate-sentry-along-with-a-feature-flag
149 integrate sentry along with a feature flag
2 parents 7cb8e5f + e39a5a2 commit 303e3c0

File tree

9 files changed

+3806
-1857
lines changed

9 files changed

+3806
-1857
lines changed

app/global-error.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use client';
2+
3+
// import * as Sentry from "@sentry/nextjs";
4+
import { useEffect } from 'react';
5+
import NextError from 'next/error';
6+
import * as Sentry from '@sentry/nextjs'
7+
8+
export default function GlobalError({
9+
error,
10+
}: {
11+
error: Error & { digest?: string };
12+
}) {
13+
useEffect(() => {
14+
Sentry.captureException(error);
15+
// console.log('Global Error ::: ', error);
16+
}, [error]);
17+
18+
return (
19+
<html>
20+
<body>
21+
{/* `NextError` is the default Next.js error page component. Its type
22+
definition requires a `statusCode` prop. However, since the App Router
23+
does not expose status codes for errors, we simply pass 0 to render a
24+
generic error message. */}
25+
<NextError statusCode={0} />
26+
</body>
27+
</html>
28+
);
29+
}

components/provider.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use client';
22

33
import React from 'react';
4+
import { ErrorBoundary } from '@sentry/nextjs';
45
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
56
import HolyLoader from 'holy-loader';
67
import { SessionProvider } from 'next-auth/react';
@@ -23,17 +24,19 @@ export default function Provider({ children }: { children: React.ReactNode }) {
2324
);
2425

2526
return (
26-
<SessionProvider>
27-
<SessionGuard>
28-
<QueryClientProvider client={client}>
29-
<RouterEvents />
30-
<HolyLoader color="var(--action-primary-success-default)" />
31-
<Tooltip.Provider>
32-
{children}
33-
<Toaster />
34-
</Tooltip.Provider>
35-
</QueryClientProvider>
36-
</SessionGuard>
37-
</SessionProvider>
27+
<ErrorBoundary>
28+
<SessionProvider>
29+
<SessionGuard>
30+
<QueryClientProvider client={client}>
31+
<RouterEvents />
32+
<HolyLoader color="var(--action-primary-success-default)" />
33+
<Tooltip.Provider>
34+
{children}
35+
<Toaster />
36+
</Tooltip.Provider>
37+
</QueryClientProvider>
38+
</SessionGuard>
39+
</SessionProvider>
40+
</ErrorBoundary>
3841
);
3942
}

instrumentation.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Sentry from '@sentry/nextjs';
2+
3+
export async function register() {
4+
const isSentryEnabled = process.env.SENTRY_ENABLED === 'true';
5+
6+
if (isSentryEnabled) {
7+
if (process.env.NEXT_RUNTIME === 'nodejs') {
8+
await import('./sentry.server.config');
9+
}
10+
11+
if (process.env.NEXT_RUNTIME === 'edge') {
12+
await import('./sentry.edge.config');
13+
}
14+
}
15+
}
16+
17+
export const onRequestError = Sentry.captureRequestError;

next.config.mjs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {withSentryConfig} from '@sentry/nextjs';
12
/** @type {import('next').NextConfig} */
23
import { fileURLToPath } from 'node:url';
34
import createJiti from 'jiti';
@@ -11,4 +12,37 @@ const nextConfig = withNextIntl({
1112
transpilePackages: ['opub-ui'],
1213
});
1314

14-
export default nextConfig;
15+
export default withSentryConfig(nextConfig, {
16+
// For all available options, see:
17+
// https://github.com/getsentry/sentry-webpack-plugin#options
18+
19+
org: process.env.SENTRY_ORG_NAME,
20+
project: process.env.SENTRY_PROJECT_NAME,
21+
22+
// Only print logs for uploading source maps in CI
23+
silent: !process.env.CI,
24+
25+
// For all available options, see:
26+
// https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/
27+
28+
// Upload a larger set of source maps for prettier stack traces (increases build time)
29+
widenClientFileUpload: true,
30+
31+
// Uncomment to route browser requests to Sentry through a Next.js rewrite to circumvent ad-blockers.
32+
// This can increase your server load as well as your hosting bill.
33+
// Note: Check that the configured route will not match with your Next.js middleware, otherwise reporting of client-
34+
// side errors will fail.
35+
// tunnelRoute: "/monitoring",
36+
37+
// Hides source maps from generated client bundles
38+
hideSourceMaps: true,
39+
40+
// Automatically tree-shake Sentry logger statements to reduce bundle size
41+
disableLogger: true,
42+
43+
// Enables automatic instrumentation of Vercel Cron Monitors. (Does not yet work with App Router route handlers.)
44+
// See the following for more information:
45+
// https://docs.sentry.io/product/crons/
46+
// https://vercel.com/docs/cron-jobs
47+
automaticVercelMonitors: true,
48+
}, process.env.SENTRY_FEATURE_ENABLED);

0 commit comments

Comments
 (0)