captureconsole.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { defineIntegration, convertIntegrationFnToClass, getClient, withScope, captureMessage, captureException } from '@sentry/core';
  2. import { CONSOLE_LEVELS, GLOBAL_OBJ, addConsoleInstrumentationHandler, severityLevelFromString, addExceptionMechanism, safeJoin } from '@sentry/utils';
  3. const INTEGRATION_NAME = 'CaptureConsole';
  4. const _captureConsoleIntegration = ((options = {}) => {
  5. const levels = options.levels || CONSOLE_LEVELS;
  6. return {
  7. name: INTEGRATION_NAME,
  8. // TODO v8: Remove this
  9. setupOnce() {}, // eslint-disable-line @typescript-eslint/no-empty-function
  10. setup(client) {
  11. if (!('console' in GLOBAL_OBJ)) {
  12. return;
  13. }
  14. addConsoleInstrumentationHandler(({ args, level }) => {
  15. if (getClient() !== client || !levels.includes(level)) {
  16. return;
  17. }
  18. consoleHandler(args, level);
  19. });
  20. },
  21. };
  22. }) ;
  23. const captureConsoleIntegration = defineIntegration(_captureConsoleIntegration);
  24. /**
  25. * Send Console API calls as Sentry Events.
  26. * @deprecated Use `captureConsoleIntegration()` instead.
  27. */
  28. // eslint-disable-next-line deprecation/deprecation
  29. const CaptureConsole = convertIntegrationFnToClass(
  30. INTEGRATION_NAME,
  31. captureConsoleIntegration,
  32. )
  33. ;
  34. function consoleHandler(args, level) {
  35. const captureContext = {
  36. level: severityLevelFromString(level),
  37. extra: {
  38. arguments: args,
  39. },
  40. };
  41. withScope(scope => {
  42. scope.addEventProcessor(event => {
  43. event.logger = 'console';
  44. addExceptionMechanism(event, {
  45. handled: false,
  46. type: 'console',
  47. });
  48. return event;
  49. });
  50. if (level === 'assert' && args[0] === false) {
  51. const message = `Assertion failed: ${safeJoin(args.slice(1), ' ') || 'console.assert'}`;
  52. scope.setExtra('arguments', args.slice(1));
  53. captureMessage(message, captureContext);
  54. return;
  55. }
  56. const error = args.find(arg => arg instanceof Error);
  57. if (level === 'error' && error) {
  58. captureException(error, captureContext);
  59. return;
  60. }
  61. const message = safeJoin(args, ' ');
  62. captureMessage(message, captureContext);
  63. });
  64. }
  65. export { CaptureConsole, captureConsoleIntegration };
  66. //# sourceMappingURL=captureconsole.js.map