aggregate-errors.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. Object.defineProperty(exports, '__esModule', { value: true });
  2. const is = require('./is.js');
  3. const string = require('./string.js');
  4. /**
  5. * Creates exceptions inside `event.exception.values` for errors that are nested on properties based on the `key` parameter.
  6. */
  7. function applyAggregateErrorsToEvent(
  8. exceptionFromErrorImplementation,
  9. parser,
  10. maxValueLimit = 250,
  11. key,
  12. limit,
  13. event,
  14. hint,
  15. ) {
  16. if (!event.exception || !event.exception.values || !hint || !is.isInstanceOf(hint.originalException, Error)) {
  17. return;
  18. }
  19. // Generally speaking the last item in `event.exception.values` is the exception originating from the original Error
  20. const originalException =
  21. event.exception.values.length > 0 ? event.exception.values[event.exception.values.length - 1] : undefined;
  22. // We only create exception grouping if there is an exception in the event.
  23. if (originalException) {
  24. event.exception.values = truncateAggregateExceptions(
  25. aggregateExceptionsFromError(
  26. exceptionFromErrorImplementation,
  27. parser,
  28. limit,
  29. hint.originalException ,
  30. key,
  31. event.exception.values,
  32. originalException,
  33. 0,
  34. ),
  35. maxValueLimit,
  36. );
  37. }
  38. }
  39. function aggregateExceptionsFromError(
  40. exceptionFromErrorImplementation,
  41. parser,
  42. limit,
  43. error,
  44. key,
  45. prevExceptions,
  46. exception,
  47. exceptionId,
  48. ) {
  49. if (prevExceptions.length >= limit + 1) {
  50. return prevExceptions;
  51. }
  52. let newExceptions = [...prevExceptions];
  53. if (is.isInstanceOf(error[key], Error)) {
  54. applyExceptionGroupFieldsForParentException(exception, exceptionId);
  55. const newException = exceptionFromErrorImplementation(parser, error[key]);
  56. const newExceptionId = newExceptions.length;
  57. applyExceptionGroupFieldsForChildException(newException, key, newExceptionId, exceptionId);
  58. newExceptions = aggregateExceptionsFromError(
  59. exceptionFromErrorImplementation,
  60. parser,
  61. limit,
  62. error[key],
  63. key,
  64. [newException, ...newExceptions],
  65. newException,
  66. newExceptionId,
  67. );
  68. }
  69. // This will create exception grouping for AggregateErrors
  70. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError
  71. if (Array.isArray(error.errors)) {
  72. error.errors.forEach((childError, i) => {
  73. if (is.isInstanceOf(childError, Error)) {
  74. applyExceptionGroupFieldsForParentException(exception, exceptionId);
  75. const newException = exceptionFromErrorImplementation(parser, childError);
  76. const newExceptionId = newExceptions.length;
  77. applyExceptionGroupFieldsForChildException(newException, `errors[${i}]`, newExceptionId, exceptionId);
  78. newExceptions = aggregateExceptionsFromError(
  79. exceptionFromErrorImplementation,
  80. parser,
  81. limit,
  82. childError,
  83. key,
  84. [newException, ...newExceptions],
  85. newException,
  86. newExceptionId,
  87. );
  88. }
  89. });
  90. }
  91. return newExceptions;
  92. }
  93. function applyExceptionGroupFieldsForParentException(exception, exceptionId) {
  94. // Don't know if this default makes sense. The protocol requires us to set these values so we pick *some* default.
  95. exception.mechanism = exception.mechanism || { type: 'generic', handled: true };
  96. exception.mechanism = {
  97. ...exception.mechanism,
  98. is_exception_group: true,
  99. exception_id: exceptionId,
  100. };
  101. }
  102. function applyExceptionGroupFieldsForChildException(
  103. exception,
  104. source,
  105. exceptionId,
  106. parentId,
  107. ) {
  108. // Don't know if this default makes sense. The protocol requires us to set these values so we pick *some* default.
  109. exception.mechanism = exception.mechanism || { type: 'generic', handled: true };
  110. exception.mechanism = {
  111. ...exception.mechanism,
  112. type: 'chained',
  113. source,
  114. exception_id: exceptionId,
  115. parent_id: parentId,
  116. };
  117. }
  118. /**
  119. * Truncate the message (exception.value) of all exceptions in the event.
  120. * Because this event processor is ran after `applyClientOptions`,
  121. * we need to truncate the message of the added exceptions here.
  122. */
  123. function truncateAggregateExceptions(exceptions, maxValueLength) {
  124. return exceptions.map(exception => {
  125. if (exception.value) {
  126. exception.value = string.truncate(exception.value, maxValueLength);
  127. }
  128. return exception;
  129. });
  130. }
  131. exports.applyAggregateErrorsToEvent = applyAggregateErrorsToEvent;
  132. //# sourceMappingURL=aggregate-errors.js.map