How to use @sentry/utils - 10 common examples

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 / browser / src / integrations / instrumenthandlers.ts View on Github external
function consoleBreadcrumb(handlerData: { [key: string]: any }): void {
  const breadcrumb = {
    category: 'console',
    data: {
      extra: {
        arguments: normalize(handlerData.args, 3),
      },
      logger: 'console',
    },
    level: Severity.fromString(handlerData.level),
    message: safeJoin(handlerData.args, ' '),
  };

  if (handlerData.level === 'assert') {
    if (handlerData.args[0] === false) {
      breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`;
      breadcrumb.data.extra.arguments = normalize(handlerData.args.slice(1), 3);
    } else {
      // Don't capture a breadcrumb for passed assertions
      return;
    }
  }
github getsentry / sentry-javascript / packages / browser / src / integrations / instrumenthandlers.ts View on Github external
const breadcrumb = {
    category: 'console',
    data: {
      extra: {
        arguments: normalize(handlerData.args, 3),
      },
      logger: 'console',
    },
    level: Severity.fromString(handlerData.level),
    message: safeJoin(handlerData.args, ' '),
  };

  if (handlerData.level === 'assert') {
    if (handlerData.args[0] === false) {
      breadcrumb.message = `Assertion failed: ${safeJoin(handlerData.args.slice(1), ' ') || 'console.assert'}`;
      breadcrumb.data.extra.arguments = normalize(handlerData.args.slice(1), 3);
    } else {
      // Don't capture a breadcrumb for passed assertions
      return;
    }
  }

  getCurrentHub().addBreadcrumb(breadcrumb, {
    input: handlerData.args,
    level: handlerData.level,
  });
}
github getsentry / sentry-javascript / packages / integrations / src / tracing.ts View on Github external
fill(
      xhrproto,
      'open',
      originalOpen =>
        function(this: XMLHttpRequest, ...args: any[]): void {
          // @ts-ignore
          const self = getCurrentHub().getIntegration(Tracing);
          if (self) {
            self._xhrUrl = args[1] as string;
          }
          // tslint:disable-next-line: no-unsafe-any
          return originalOpen.apply(this, args);
        },
    );

    fill(
      xhrproto,
      'send',
      originalSend =>
        function(this: XMLHttpRequest, ...args: any[]): void {
          // @ts-ignore
          const self = getCurrentHub().getIntegration(Tracing);
          if (self && self._xhrUrl && self._options.tracingOrigins) {
            const url = self._xhrUrl;
            const headers = getCurrentHub().traceHeaders();
            // tslint:disable-next-line: prefer-for-of
            const isWhitelisted = self._options.tracingOrigins.some((origin: string | RegExp) =>
              isMatchingPattern(url, origin),
            );

            if (isWhitelisted && this.setRequestHeader) {
              Object.keys(headers).forEach(key => {
github getsentry / sentry-javascript / packages / browser / src / integrations / breadcrumbs.ts View on Github external
if (url) {
          // coerce to string (this is what pushState does)
          const to = String(url);
          const handlerData = {
            from: lastHref,
            to,
          };
          // keep track of the current URL state, as we always receive only the updated state
          lastHref = to;
          triggerHandlers(handlerData);
        }
        return originalHistoryFunction.apply(this, args);
      };
    }

    fill(global.history, 'pushState', historyReplacementFunction);
    fill(global.history, 'replaceState', historyReplacementFunction);
  }
github getsentry / sentry-javascript / packages / node / src / integrations / console.ts View on Github external
public setupOnce(): void {
    const nativeModule = require('module');
    fill(nativeModule, '_load', loadWrapper(nativeModule));
    // special case: since console is built-in and app-level code won't require() it, do that here
    require('console');
  }
}
github getsentry / sentry-javascript / packages / integrations / src / tracing.ts View on Github external
private _traceXHR(getCurrentHub: () => Hub): void {
    if (!('XMLHttpRequest' in getGlobalObject())) {
      return;
    }

    const xhrproto = XMLHttpRequest.prototype;

    fill(
      xhrproto,
      'open',
      originalOpen =>
        function(this: XMLHttpRequest, ...args: any[]): void {
          // @ts-ignore
          const self = getCurrentHub().getIntegration(Tracing);
          if (self) {
            self._xhrUrl = args[1] as string;
          }
          // tslint:disable-next-line: no-unsafe-any
          return originalOpen.apply(this, args);
        },
    );

    fill(
      xhrproto,
github getsentry / sentry-javascript / packages / browser / src / integrations / breadcrumbs.ts View on Github external
['debug', 'info', 'warn', 'error', 'log', 'assert'].forEach(function(level: string): void {
      if (!(level in global.console)) {
        return;
      }

      fill(global.console, level, function(originalConsoleLevel: () => any): Function {
        return function(...args: any[]): void {
          const handlerData = {
            args,
            level,
          };
          triggerHandlers(handlerData);

          // this fails for some browsers. :(
          if (originalConsoleLevel) {
            Function.prototype.apply.call(originalConsoleLevel, global.console, args);
          }
        };
      });
    });
  }
github getsentry / sentry-javascript / packages / integrations / src / tracing.ts View on Github external
private _traceFetch(getCurrentHub: () => Hub): void {
    if (!supportsNativeFetch()) {
      return;
    }

    // tslint:disable: only-arrow-functions
    fill(getGlobalObject(), 'fetch', function(originalFetch: () => void): () => void {
      return function(...args: any[]): void {
        // @ts-ignore
        const self = getCurrentHub().getIntegration(Tracing);
        if (self && self._options.tracingOrigins) {
          const url = args[0] as string;
          const options = (args[1] = (args[1] as { [key: string]: any }) || {});

          let whiteListed = false;
          self._options.tracingOrigins.forEach((whiteListUrl: string | RegExp) => {
            if (!whiteListed) {
              whiteListed = isMatchingPattern(url, whiteListUrl);
            }
          });

          if (whiteListed) {
            if (options.headers) {
github getsentry / sentry-javascript / packages / browser / src / client.ts View on Github external
public showReportDialog(options: ReportDialogOptions = {}): void {
    // doesn't work without a document (React Native)
    const document = getGlobalObject().document;
    if (!document) {
      return;
    }

    if (!this._isEnabled()) {
      logger.error('Trying to call showReportDialog with Sentry Client is disabled');
      return;
    }

    const dsn = options.dsn || this.getDsn();

    if (!options.eventId) {
      logger.error('Missing `eventId` option in showReportDialog call');
      return;
    }
github getsentry / sentry-javascript / packages / integrations / src / ember.ts View on Github external
public constructor(options: { Ember?: any } = {}) {
    // tslint:disable-next-line: no-unsafe-any
    this._Ember = options.Ember || getGlobalObject().Ember;
  }