1

I am using create-react-app to build my application and new relic's noticeError() to log errors.

Below, you can see an example of how I log errors:

  var err = new Error('Report caught error to New Relic');
  
  // exported from logger.ts
  logError(err, {
    hint:
      'Check the validateToken() function. The JWT token might have expired',
    filePath: 'src/routes/Routes.tsx',
  });

logger.ts

import { getLocalStorage } from '../utils/localStorageHandler';
import LOCALSTORAGE_KEYS from '../constants/localStorageKeys';
import { CandidatePersonalDetail } from '../graphql/declarations';

interface Attributes {
  errorType: string;
  mobile?: string;
  userID?: number;
  role?: string;
  name?: string;
}

// Define and export your error types from here
export const ERROR_TYPES = {
  API_ERROR: 'API_ERROR',
};

export const logError = (
  err: any,
  customAttributes?: { [key: string]: string | number },
  callback?: () => void,
): void => {
  const attributes: Attributes = {
    errorType: ERROR_TYPES.API_ERROR,
    ...customAttributes,
  };

  const personalDetails:
    | CandidatePersonalDetail['candidate']
    | null = getLocalStorage(LOCALSTORAGE_KEYS.USER_PERSONAL_DETAILS);

  // If personal details exist in local storage
  if (personalDetails && Object.keys(personalDetails).length) {
    if (personalDetails.mobile) {
      attributes.mobile = personalDetails.mobile;
    }

    if ((personalDetails as any).role) {
      attributes.userID = personalDetails.id;
      attributes.name = `${personalDetails.firstName} ${personalDetails.lastName}`;
      attributes.role = (personalDetails as any).role;
    }
  }

  (window as any).newrelic.noticeError(err, attributes);

  if (callback) {
    callback();
  }
};

I am manually adding the file path to help the developers as much as I can to debug. Is it possible to automate this by accessing the file path whenever logError() function is called so that I can pass it as a custom attribute to new relic? I am ok with absolute or relative paths. Thanks in anticipation.

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.