eventbuilder.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const is = require('./is.js');
  3. const misc = require('./misc.js');
  4. const normalize = require('./normalize.js');
  5. const object = require('./object.js');
  6. /**
  7. * Extracts stack frames from the error.stack string
  8. */
  9. function parseStackFrames(stackParser, error) {
  10. return stackParser(error.stack || '', 1);
  11. }
  12. /**
  13. * Extracts stack frames from the error and builds a Sentry Exception
  14. */
  15. function exceptionFromError(stackParser, error) {
  16. const exception = {
  17. type: error.name || error.constructor.name,
  18. value: error.message,
  19. };
  20. const frames = parseStackFrames(stackParser, error);
  21. if (frames.length) {
  22. exception.stacktrace = { frames };
  23. }
  24. return exception;
  25. }
  26. function getMessageForObject(exception) {
  27. if ('name' in exception && typeof exception.name === 'string') {
  28. let message = `'${exception.name}' captured as exception`;
  29. if ('message' in exception && typeof exception.message === 'string') {
  30. message += ` with message '${exception.message}'`;
  31. }
  32. return message;
  33. } else if ('message' in exception && typeof exception.message === 'string') {
  34. return exception.message;
  35. } else {
  36. // This will allow us to group events based on top-level keys
  37. // which is much better than creating new group when any key/value change
  38. return `Object captured as exception with keys: ${object.extractExceptionKeysForMessage(
  39. exception ,
  40. )}`;
  41. }
  42. }
  43. /**
  44. * Builds and Event from a Exception
  45. *
  46. * TODO(v8): Remove getHub fallback
  47. * @hidden
  48. */
  49. function eventFromUnknownInput(
  50. getHubOrClient,
  51. stackParser,
  52. exception,
  53. hint,
  54. ) {
  55. const client =
  56. typeof getHubOrClient === 'function'
  57. ? // eslint-disable-next-line deprecation/deprecation
  58. getHubOrClient().getClient()
  59. : getHubOrClient;
  60. let ex = exception;
  61. const providedMechanism =
  62. hint && hint.data && (hint.data ).mechanism;
  63. const mechanism = providedMechanism || {
  64. handled: true,
  65. type: 'generic',
  66. };
  67. let extras;
  68. if (!is.isError(exception)) {
  69. if (is.isPlainObject(exception)) {
  70. const normalizeDepth = client && client.getOptions().normalizeDepth;
  71. extras = { ['__serialized__']: normalize.normalizeToSize(exception , normalizeDepth) };
  72. const message = getMessageForObject(exception);
  73. ex = (hint && hint.syntheticException) || new Error(message);
  74. (ex ).message = message;
  75. } else {
  76. // This handles when someone does: `throw "something awesome";`
  77. // We use synthesized Error here so we can extract a (rough) stack trace.
  78. ex = (hint && hint.syntheticException) || new Error(exception );
  79. (ex ).message = exception ;
  80. }
  81. mechanism.synthetic = true;
  82. }
  83. const event = {
  84. exception: {
  85. values: [exceptionFromError(stackParser, ex )],
  86. },
  87. };
  88. if (extras) {
  89. event.extra = extras;
  90. }
  91. misc.addExceptionTypeValue(event, undefined, undefined);
  92. misc.addExceptionMechanism(event, mechanism);
  93. return {
  94. ...event,
  95. event_id: hint && hint.event_id,
  96. };
  97. }
  98. /**
  99. * Builds and Event from a Message
  100. * @hidden
  101. */
  102. function eventFromMessage(
  103. stackParser,
  104. message,
  105. // eslint-disable-next-line deprecation/deprecation
  106. level = 'info',
  107. hint,
  108. attachStacktrace,
  109. ) {
  110. const event = {
  111. event_id: hint && hint.event_id,
  112. level,
  113. };
  114. if (attachStacktrace && hint && hint.syntheticException) {
  115. const frames = parseStackFrames(stackParser, hint.syntheticException);
  116. if (frames.length) {
  117. event.exception = {
  118. values: [
  119. {
  120. value: message,
  121. stacktrace: { frames },
  122. },
  123. ],
  124. };
  125. }
  126. }
  127. if (is.isParameterizedString(message)) {
  128. const { __sentry_template_string__, __sentry_template_values__ } = message;
  129. event.logentry = {
  130. message: __sentry_template_string__,
  131. params: __sentry_template_values__,
  132. };
  133. return event;
  134. }
  135. event.message = message;
  136. return event;
  137. }
  138. exports.eventFromMessage = eventFromMessage;
  139. exports.eventFromUnknownInput = eventFromUnknownInput;
  140. exports.exceptionFromError = exceptionFromError;
  141. exports.parseStackFrames = parseStackFrames;
  142. //# sourceMappingURL=eventbuilder.js.map