eventbuilder.js 4.0 KB

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