How to use the @sentry/utils.isError function in @sentry/utils

To help you get started, we’ve selected a few @sentry/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github getsentry / sentry-javascript / packages / integrations / src / extraerrordata.ts View on Github external
private _extractErrorData(error: ExtendedError): { [key: string]: unknown } | null {
    let result = null;
    // We are trying to enhance already existing event, so no harm done if it won't succeed
    try {
      const nativeKeys = ['name', 'message', 'stack', 'line', 'column', 'fileName', 'lineNumber', 'columnNumber'];
      const errorKeys = Object.getOwnPropertyNames(error).filter(key => nativeKeys.indexOf(key) === -1);

      if (errorKeys.length) {
        const extraErrorInfo: { [key: string]: unknown } = {};
        for (const key of errorKeys) {
          let value = error[key];
          if (isError(value)) {
            value = (value as Error).toString();
          }
          // tslint:disable:no-unsafe-any
          extraErrorInfo[key] = value;
        }
        result = extraErrorInfo;
      }
    } catch (oO) {
      logger.error('Unable to extract extra data from the Error object:', oO);
    }

    return result;
  }
}
github getsentry / sentry-javascript / packages / browser / src / eventbuilder.ts View on Github external
return event;
  }
  if (isDOMError(exception as DOMError) || isDOMException(exception as DOMException)) {
    // If it is a DOMError or DOMException (which are legacy APIs, but still supported in some browsers)
    // then we just extract the name and message, as they don't provide anything else
    // https://developer.mozilla.org/en-US/docs/Web/API/DOMError
    // https://developer.mozilla.org/en-US/docs/Web/API/DOMException
    const domException = exception as DOMException;
    const name = domException.name || (isDOMError(domException) ? 'DOMError' : 'DOMException');
    const message = domException.message ? `${name}: ${domException.message}` : name;

    event = eventFromString(message, syntheticException, options);
    addExceptionTypeValue(event, message);
    return event;
  }
  if (isError(exception as Error)) {
    // we have a real Error object, do nothing
    event = eventFromStacktrace(computeStackTrace(exception as Error));
    return event;
  }
  if (isPlainObject(exception) || isEvent(exception)) {
    // If it is plain Object or Event, serialize it manually and extract options
    // This will allow us to group events based on top-level keys
    // which is much better than creating new group when any key/value change
    const objectException = exception as {};
    event = eventFromPlainObject(objectException, syntheticException, options.rejection);
    addExceptionMechanism(event, {
      synthetic: true,
    });
    return event;
  }
github getsentry / sentry-javascript / packages / browser / src / tracekit.ts View on Github external
function _traceKitWindowOnError(message: any, url: any, lineNo: any, columnNo: any, errorObj: any) {
    var stack = null;
    // If 'errorObj' is ErrorEvent, get real Error from inside
    errorObj = isErrorEvent(errorObj) ? errorObj.error : errorObj;
    // If 'message' is ErrorEvent, get real message from inside
    message = isErrorEvent(message) ? message.message : message;

    if (lastExceptionStack) {
      TraceKit._computeStackTrace._augmentStackTraceWithInitialElement(lastExceptionStack, url, lineNo, message);
      processLastException();
    } else if (errorObj && isError(errorObj)) {
      stack = TraceKit._computeStackTrace(errorObj);
      stack.mechanism = 'onerror';
      _notifyHandlers(stack, true, errorObj);
    } else {
      var location: any = {
        url: url,
        line: lineNo,
        column: columnNo,
      };

      var name;
      var msg = message; // must be new var or will modify original `arguments`
      if ({}.toString.call(message) === '[object String]') {
        var groups = message.match(ERROR_TYPES_RE);
        if (groups) {
          name = groups[1];
github getsentry / sentry-javascript / packages / node / src / backend.ts View on Github external
public eventFromException(exception: any, hint?: EventHint): PromiseLike {
    let ex: any = exception;
    const mechanism: Mechanism = {
      handled: true,
      type: 'generic',
    };

    if (!isError(exception)) {
      if (isPlainObject(exception)) {
        // This will allow us to group events based on top-level keys
        // which is much better than creating new group when any key/value change
        const message = `Non-Error exception captured with keys: ${extractExceptionKeysForMessage(exception)}`;

        getCurrentHub().configureScope(scope => {
          scope.setExtra('__serialized__', normalizeToSize(exception as {}));
        });

        ex = (hint && hint.syntheticException) || new Error(message);
        (ex as Error).message = message;
      } else {
        // This handles when someone does: `throw "something awesome";`
        // We use synthesized Error here so we can extract a (rough) stack trace.
        ex = (hint && hint.syntheticException) || new Error(exception as string);
      }
github getsentry / sentry-javascript / packages / integrations / src / extraerrordata.ts View on Github external
public enhanceEventWithErrorData(event: Event, hint?: EventHint): Event {
    if (!hint || !hint.originalException || !isError(hint.originalException)) {
      return event;
    }
    const name = (hint.originalException as ExtendedError).name || hint.originalException.constructor.name;

    const errorData = this._extractErrorData(hint.originalException as ExtendedError);

    if (errorData) {
      let contexts = {
        ...event.contexts,
      };

      const normalizedErrorData = normalize(errorData, this._options.depth);
      if (isPlainObject(normalizedErrorData)) {
        contexts = {
          ...event.contexts,
          [name]: {