The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { useCallback, useEffect } from 'react';
import { checkIfItsAViteStaleChunkLazyLoadingError } from '@/error-handler/utils/checkIfItsAViteStaleChunkLazyLoadingError';
import { getToastOptionsFromError } from '@/error-handler/utils/getToastOptionsFromError';
import { isMonacoCancellationError } from '@/error-handler/utils/isMonacoCancellationError';
import {
CombinedGraphQLErrors,
CombinedProtocolErrors,
LinkError,
LocalStateError,
ServerError,
ServerParseError,
UnconventionalError,
} from '@apollo/client/errors';
import { isDefined, type CustomError } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/components/feedback';
const isApolloError = (error: unknown): boolean =>
CombinedGraphQLErrors.is(error) ||
CombinedProtocolErrors.is(error) ||
LinkError.is(error) ||
LocalStateError.is(error) ||
ServerError.is(error) ||
ServerParseError.is(error) ||
UnconventionalError.is(error);
const hasErrorCode = (
error: CustomError | any,
): error is CustomError & { code: string } => {
return 'code' in error && isDefined(error.code);
};
export const PromiseRejectionEffect = () => {
const { enqueueToast } = useToast();
const handlePromiseRejection = useCallback(
async (event: PromiseRejectionEvent) => {
const error = event.reason;
if (error instanceof Error && isMonacoCancellationError(error)) {
return;
}
if (isApolloError(error)) {
enqueueToast(getToastOptionsFromError({ error }));
return; // already handled by apolloLink
}
const isAbortError =
error?.networkError?.name === 'AbortError' ||
error?.name === 'AbortError' ||
(error instanceof TypeError &&
error.message.toLowerCase() === 'cancelled');
const isViteStaleChunkLazyLoadingError =
error instanceof Error &&
checkIfItsAViteStaleChunkLazyLoadingError(error);
if (!isAbortError && !isViteStaleChunkLazyLoadingError) {
enqueueToast(getToastOptionsFromError({ error }));
}
try {
const { captureException } = await import('@sentry/react');
captureException(error, (scope) => {
scope.setExtras({ mechanism: 'onUnhandle' });
const fingerprint = hasErrorCode(error) ? error.code : error.message;
scope.setFingerprint([fingerprint]);
error.name = error.message;
return scope;
});
} catch (sentryError) {
// oxlint-disable-next-line no-console
console.error('Failed to capture exception with Sentry:', sentryError);
}
},
[enqueueToast],
);
useEffect(() => {
window.addEventListener('unhandledrejection', handlePromiseRejection);
return () => {
window.removeEventListener('unhandledrejection', handlePromiseRejection);
};
}, [handlePromiseRejection]);
return <></>;
};