File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ "use client" ;
2+
3+ import type { Properties } from "posthog-js" ;
4+ import { usePostHog } from "posthog-js/react" ;
5+
6+ const NON_WORD = / \W + / g;
7+
8+ const internalError = ( {
9+ error,
10+ type,
11+ context,
12+ forceSendInDev = false ,
13+ } : {
14+ error : unknown ;
15+ type ?: string ;
16+ context ?: Properties ;
17+ forceSendInDev ?: boolean ;
18+ } ) : void => {
19+ if ( process . env . NODE_ENV === "development" && forceSendInDev !== true ) {
20+ console . error ( error ) ;
21+ } else {
22+ const posthog = usePostHog ( ) ;
23+ type = type || "Internal Error" ;
24+ const slugType = type . replaceAll ( NON_WORD , "-" ) . toLowerCase ( ) ;
25+ context = {
26+ type,
27+ ...( context || { } ) ,
28+ } ;
29+
30+ if ( typeof error === "string" ) {
31+ error = new Error ( error ) ;
32+ } else if ( ! ( error instanceof Error ) ) {
33+ try {
34+ const serialized = JSON . stringify ( error ) ;
35+ error = new Error ( serialized ) ;
36+ } catch {
37+ error = new Error ( typeof error ) ;
38+ }
39+ }
40+ posthog . captureException ( error , { ...context , type : slugType } ) ;
41+ }
42+ } ;
43+
44+ export default internalError ;
You can’t perform that action at this time.
0 commit comments