index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* global exports */
  2. Object.defineProperty(exports, '__esModule', {value: true});
  3. const tslib_1 = require('tslib');
  4. const hub_1 = require('@sentry/hub');
  5. /**
  6. * This calls a function on the current hub.
  7. * @param method function to call on hub.
  8. * @param args to pass to function.
  9. */
  10. function callOnHub(method) {
  11. const args = [];
  12. for (let _i = 1; _i < arguments.length; _i++) {
  13. args[_i - 1] = arguments[_i];
  14. }
  15. const hub = hub_1.getCurrentHub();
  16. if (hub && hub[method]) {
  17. // tslint:disable-next-line:no-unsafe-any
  18. return hub[method].apply(hub, tslib_1.__spread(args));
  19. }
  20. throw new Error(
  21. 'No hub defined or ' + method + ' was not found on the hub, please open a bug report.'
  22. );
  23. }
  24. /**
  25. * Captures an exception event and sends it to Sentry.
  26. *
  27. * @param exception An exception-like object.
  28. * @returns The generated eventId.
  29. */
  30. function captureException(exception) {
  31. let syntheticException;
  32. try {
  33. throw new Error('Sentry syntheticException');
  34. } catch (error) {
  35. syntheticException = error;
  36. }
  37. return callOnHub('captureException', exception, {
  38. originalException: exception,
  39. syntheticException,
  40. });
  41. }
  42. exports.captureException = captureException;
  43. /**
  44. * Captures a message event and sends it to Sentry.
  45. *
  46. * @param message The message to send to Sentry.
  47. * @param level Define the level of the message.
  48. * @returns The generated eventId.
  49. */
  50. function captureMessage(message, level) {
  51. let syntheticException;
  52. try {
  53. throw new Error(message);
  54. } catch (exception) {
  55. syntheticException = exception;
  56. }
  57. return callOnHub('captureMessage', message, level, {
  58. originalException: message,
  59. syntheticException,
  60. });
  61. }
  62. exports.captureMessage = captureMessage;
  63. /**
  64. * Captures a manually created event and sends it to Sentry.
  65. *
  66. * @param event The event to send to Sentry.
  67. * @returns The generated eventId.
  68. */
  69. function captureEvent(event) {
  70. return callOnHub('captureEvent', event);
  71. }
  72. exports.captureEvent = captureEvent;
  73. /**
  74. * Records a new breadcrumb which will be attached to future events.
  75. *
  76. * Breadcrumbs will be added to subsequent events to provide more context on
  77. * user's actions prior to an error or crash.
  78. *
  79. * @param breadcrumb The breadcrumb to record.
  80. */
  81. function addBreadcrumb(breadcrumb) {
  82. callOnHub('addBreadcrumb', breadcrumb);
  83. }
  84. exports.addBreadcrumb = addBreadcrumb;
  85. /**
  86. * Callback to set context information onto the scope.
  87. * @param callback Callback function that receives Scope.
  88. */
  89. function configureScope(callback) {
  90. callOnHub('configureScope', callback);
  91. }
  92. exports.configureScope = configureScope;
  93. /**
  94. * Creates a new scope with and executes the given operation within.
  95. * The scope is automatically removed once the operation
  96. * finishes or throws.
  97. *
  98. * This is essentially a convenience function for:
  99. *
  100. * pushScope();
  101. * callback();
  102. * popScope();
  103. *
  104. * @param callback that will be enclosed into push/popScope.
  105. */
  106. function withScope(callback) {
  107. callOnHub('withScope', callback);
  108. }
  109. exports.withScope = withScope;
  110. /**
  111. * Calls a function on the latest client. Use this with caution, it's meant as
  112. * in "internal" helper so we don't need to expose every possible function in
  113. * the shim. It is not guaranteed that the client actually implements the
  114. * function.
  115. *
  116. * @param method The method to call on the client/client.
  117. * @param args Arguments to pass to the client/fontend.
  118. */
  119. function _callOnClient(method) {
  120. const args = [];
  121. for (let _i = 1; _i < arguments.length; _i++) {
  122. args[_i - 1] = arguments[_i];
  123. }
  124. callOnHub.apply(void 0, tslib_1.__spread(['_invokeClient', method], args));
  125. }
  126. exports._callOnClient = _callOnClient;
  127. // # sourceMappingURL=index.js.map