-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch errors in react server components (#4857)
* Catch errors in react server components * clean ScoutProfile * Log in the FE that it was an error * Update
- Loading branch information
1 parent
0635898
commit 9edccb3
Showing
12 changed files
with
169 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 14 additions & 10 deletions
24
apps/scoutgame/components/[username]/components/PublicScoutProfile/PublicScoutProfile.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use client'; | ||
|
||
import { log } from '@charmverse/core/log'; | ||
import { Alert } from '@mui/material'; | ||
import { useEffect } from 'react'; | ||
|
||
export function ErrorSSRMessage({ message }: { message?: string }) { | ||
useEffect(() => { | ||
log.error('Error in SSR data fetching. Please refresh.'); | ||
}, []); | ||
|
||
return ( | ||
<Alert severity='warning'> | ||
{message || 'An error occured while loading your data. Please try to refresh or contact us on discord'} | ||
</Alert> | ||
); | ||
} |
9 changes: 8 additions & 1 deletion
9
apps/scoutgame/components/home/components/BuildersCarousel/TodaysHotBuildersCarousel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
'use server'; | ||
|
||
import { ErrorSSRMessage } from 'components/common/ErrorSSRMessage'; | ||
import { getTodaysHotBuilders } from 'lib/builders/getTodaysHotBuilders'; | ||
import { safeAwaitSSRData } from 'lib/utils/async'; | ||
|
||
import { BuildersCarousel } from './BuildersCarousel'; | ||
|
||
export async function TodaysHotBuildersCarousel() { | ||
const builders = await getTodaysHotBuilders(); | ||
const [error, builders] = await safeAwaitSSRData(getTodaysHotBuilders()); | ||
|
||
if (error) { | ||
return <ErrorSSRMessage />; | ||
} | ||
|
||
return <BuildersCarousel builders={builders} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 13 additions & 17 deletions
30
apps/scoutgame/components/profile/components/ScoutProfile/ScoutProfile.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { prisma } from '@charmverse/core/prisma-client'; | ||
|
||
import { BasicUserInfoSelect } from 'lib/users/queries'; | ||
|
||
export async function findScoutOrThrow(scoutId: string) { | ||
return prisma.scout.findUniqueOrThrow({ | ||
where: { | ||
id: scoutId | ||
}, | ||
select: BasicUserInfoSelect | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { prisma } from '@charmverse/core/prisma-client'; | ||
import { currentSeason } from '@packages/scoutgame/dates'; | ||
|
||
export async function getUserSeasonStats(_userId: string) { | ||
return prisma.userSeasonStats.findUnique({ | ||
where: { | ||
userId_season: { | ||
userId: _userId, | ||
season: currentSeason | ||
} | ||
}, | ||
select: { | ||
pointsEarnedAsScout: true | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { UnknownError } from '@charmverse/core/errors'; | ||
import { log } from '@charmverse/core/log'; | ||
import { getSession } from '@connect-shared/lib/session/getSession'; | ||
import { isSystemError } from '@root/lib/middleware/isSystemError'; | ||
import { headers } from 'next/headers'; | ||
|
||
export type MaybePromise<T> = T | Promise<T>; | ||
|
||
export async function safeAwait<T>( | ||
promise: Promise<T>, | ||
options?: { onSuccess?: (response: T) => MaybePromise<void>; onError?: (error: Error) => MaybePromise<void> } | ||
): Promise<[error: Error] | [error: null, data: T]> { | ||
try { | ||
const response = await promise; | ||
await options?.onSuccess?.(response); | ||
return [null, response]; | ||
} catch (_error: any) { | ||
await options?.onError?.(_error); | ||
return [_error]; | ||
} | ||
} | ||
|
||
export async function safeAwaitSSRData<T>(promise: Promise<T>) { | ||
return safeAwait(promise, { | ||
onError: async (err) => { | ||
const session = await getSession(); | ||
const headersList = headers(); | ||
const fullUrl = headersList.get('referer') || ''; | ||
|
||
const isValidSystemError = isSystemError(err); | ||
|
||
const errorAsSystemError = isValidSystemError ? err : new UnknownError(err.stack ?? err); | ||
|
||
const loggedInfo = { | ||
error: errorAsSystemError, | ||
stack: err.stack, | ||
userId: session.scoutId, | ||
url: fullUrl, | ||
ssr: true // Flag to identify that this error was thrown during SSR and can be looked up in DD | ||
}; | ||
|
||
log.error('Server error fetching SSR data', loggedInfo); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters