-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Log error to DD in prod * log error * add context
- Loading branch information
Showing
21 changed files
with
214 additions
and
92 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
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
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,57 @@ | ||
'use client'; | ||
|
||
import { datadogRum } from '@datadog/browser-rum'; | ||
import { isDevelopment } from 'apps/web/src/constants'; | ||
import { ReactNode, createContext, useCallback, useContext, useMemo } from 'react'; | ||
|
||
export type ErrorsContextProps = { | ||
logError: (error: unknown, message: string) => void; | ||
fullContext: string; | ||
}; | ||
|
||
export const ErrorsContext = createContext<ErrorsContextProps>({ | ||
logError: function () { | ||
return undefined; | ||
}, | ||
fullContext: '', | ||
}); | ||
|
||
export function useErrors() { | ||
const context = useContext(ErrorsContext); | ||
if (context === undefined) { | ||
throw new Error('useErrors must be used within a ErrorsProvider'); | ||
} | ||
return context; | ||
} | ||
|
||
type ErrorsProviderProps = { | ||
children?: ReactNode; | ||
context: string; | ||
}; | ||
|
||
export default function ErrorsProvider({ children, context }: ErrorsProviderProps) { | ||
const { fullContext: previousContext } = useErrors(); | ||
|
||
const fullContext = [previousContext, context].filter((c) => !!c).join('_'); | ||
|
||
const logError = useCallback( | ||
(error: unknown, message: string) => { | ||
if (isDevelopment) { | ||
console.log('\n--------------------------------------'); | ||
console.info(`Error caught with message: "${message}"`); | ||
console.error(error); | ||
console.info(`Context: "${fullContext}"`); | ||
console.log('--------------------------------------\n'); | ||
return; | ||
} | ||
datadogRum.addError(error, { context: fullContext, message: message }); | ||
}, | ||
[fullContext], | ||
); | ||
|
||
const values = useMemo(() => { | ||
return { logError, context, fullContext }; | ||
}, [context, fullContext, logError]); | ||
|
||
return <ErrorsContext.Provider value={values}>{children}</ErrorsContext.Provider>; | ||
} |
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
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
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
Oops, something went wrong.