Skip to content

Commit

Permalink
Avoid throwing not found error when not login in nextjs pages
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Oct 22, 2024
1 parent cda5630 commit 1657f8d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions apps/web/sentry.edge.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import * as Sentry from '@sentry/nextjs'
import { isReportable } from 'sentry.server.config'

Sentry.init({
dsn: process.env.SENTRY_DSN,
beforeSend(event, hint) {
const error = hint.originalException

if (!isReportable(error)) return null

return event
},

// Setting this option to true will print useful information to the console while you're setting up Sentry.
debug: false,
Expand Down
12 changes: 12 additions & 0 deletions apps/web/sentry.server.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,22 @@
// The config you add here will be used whenever the server handles a request.
// https://docs.sentry.io/platforms/javascript/guides/nextjs/

import { NotFoundError } from '@latitude-data/core/lib/errors'
import * as Sentry from '@sentry/nextjs'

export function isReportable(error: unknown): boolean {
return !(error instanceof NotFoundError)
}

Sentry.init({
dsn: process.env.SENTRY_DSN,
beforeSend(event, hint) {
const error = hint.originalException

if (!isReportable(error)) return null

return event
},

// Setting this option to true will print useful information to the console while you're setting up Sentry.
debug: false,
Expand Down
7 changes: 5 additions & 2 deletions apps/web/src/app/global-error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import { useEffect } from 'react'

import { Button } from '@latitude-data/web-ui'
import { ErrorComponent } from '@latitude-data/web-ui/browser'
import * as Sentry from '@sentry/nextjs'
import { fontMono, fontSans } from '$/helpers/fonts'
import { ROUTES } from '$/services/routes'
import Link from 'next/link'
import { NotFoundError } from '@latitude-data/core/lib/errors'
import { captureException } from '$/helpers/captureException'

export default function GlobalError({
error,
}: {
error: Error & { digest?: string }
}) {
useEffect(() => {
Sentry.captureException(error)
if (error instanceof NotFoundError) return

captureException(error)
}, [error])

return (
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/services/auth/getCurrentUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { getCurrentUserFromDB } from '$/data-access'
import { Session } from 'lucia'

import { getSession } from './getSession'
import { redirect } from 'next/navigation'
import { ROUTES } from '$/services/routes'

export type SessionData = {
session: Session
Expand All @@ -14,6 +16,11 @@ export type SessionData = {
export const getCurrentUser = cache(async () => {
const sessionData = await getSession()
const result = await getCurrentUserFromDB({ userId: sessionData?.user?.id })

if (result.error) {
return redirect(ROUTES.auth.login)
}

const { user, workspace } = result.unwrap()

return {
Expand Down

0 comments on commit 1657f8d

Please sign in to comment.