reportingobserver.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { defineIntegration, convertIntegrationFnToClass, getClient, withScope, captureMessage } from '@sentry/core';
  2. import { supportsReportingObserver, GLOBAL_OBJ } from '@sentry/utils';
  3. const WINDOW = GLOBAL_OBJ ;
  4. const INTEGRATION_NAME = 'ReportingObserver';
  5. const SETUP_CLIENTS = new WeakMap();
  6. const _reportingObserverIntegration = ((options = {}) => {
  7. const types = options.types || ['crash', 'deprecation', 'intervention'];
  8. /** Handler for the reporting observer. */
  9. function handler(reports) {
  10. if (!SETUP_CLIENTS.has(getClient() )) {
  11. return;
  12. }
  13. for (const report of reports) {
  14. withScope(scope => {
  15. scope.setExtra('url', report.url);
  16. const label = `ReportingObserver [${report.type}]`;
  17. let details = 'No details available';
  18. if (report.body) {
  19. // Object.keys doesn't work on ReportBody, as all properties are inheirted
  20. const plainBody
  21. = {};
  22. // eslint-disable-next-line guard-for-in
  23. for (const prop in report.body) {
  24. plainBody[prop] = report.body[prop];
  25. }
  26. scope.setExtra('body', plainBody);
  27. if (report.type === 'crash') {
  28. const body = report.body ;
  29. // A fancy way to create a message out of crashId OR reason OR both OR fallback
  30. details = [body.crashId || '', body.reason || ''].join(' ').trim() || details;
  31. } else {
  32. const body = report.body ;
  33. details = body.message || details;
  34. }
  35. }
  36. captureMessage(`${label}: ${details}`);
  37. });
  38. }
  39. }
  40. return {
  41. name: INTEGRATION_NAME,
  42. setupOnce() {
  43. if (!supportsReportingObserver()) {
  44. return;
  45. }
  46. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
  47. const observer = new (WINDOW ).ReportingObserver(handler, {
  48. buffered: true,
  49. types,
  50. });
  51. // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
  52. observer.observe();
  53. },
  54. setup(client) {
  55. SETUP_CLIENTS.set(client, true);
  56. },
  57. };
  58. }) ;
  59. const reportingObserverIntegration = defineIntegration(_reportingObserverIntegration);
  60. /**
  61. * Reporting API integration - https://w3c.github.io/reporting/
  62. * @deprecated Use `reportingObserverIntegration()` instead.
  63. */
  64. // eslint-disable-next-line deprecation/deprecation
  65. const ReportingObserver = convertIntegrationFnToClass(
  66. INTEGRATION_NAME,
  67. reportingObserverIntegration,
  68. )
  69. ;
  70. export { ReportingObserver, reportingObserverIntegration };
  71. //# sourceMappingURL=reportingobserver.js.map