import * as Sentry from "@sentry/react-native"; import { AxiosError } from "axios"; import Config from "react-native-config"; Sentry.init({ enabled: !__DEV__, autoInitializeNativeSdk: false, dsn: Config.Sentry, environment: __DEV__ ? "debug" : Config.RELEASE_STAGE, beforeSend: (event) => { // https://docs.sentry.io/platforms/react-native/configuration/filtering/#filtering-error-events if (event.message === "Network Error") { return null; } return event; }, }); export function errorReporting(error: AxiosError) { if (!error.response) { // Catch non AxiosError errors caused by invalid react query hooks, normally type errors Sentry.captureException(error, (scope) => { scope.setTransactionName(`Error in react query error reporting flow`); return scope; }); } else { const errorCode = error?.response?.data?.ErrorCode || error.response?.status; let url = error.response?.config?.url || ""; let grouping = ["{{ default }}"]; // ensures endpoints with specific errorCodes are grouped separately for easier debugging if (errorCode) { grouping = ["{{ default }}", errorCode.toString(), url]; } Sentry.captureException(error, (scope) => { // Sets sentry error titles and groups based on error code, method and url scope.setTransactionName(`[${errorCode} ${error.response?.config.method}] - ${url}`); if (grouping) { scope.setFingerprint(grouping); } scope.setExtras({ type: "error", data: error.response?.data, configData: error.response?.config, status: error.response?.status, }); return scope; }); } }