getThreadException.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import {Event, ExceptionType, ExceptionValue, Thread} from 'sentry/types';
  2. import {defined} from 'sentry/utils';
  3. function getException(
  4. exceptionData: ExceptionType,
  5. exceptionDataValues: ExceptionValue[],
  6. thread: Thread
  7. ) {
  8. if (exceptionDataValues.length === 1 && !exceptionDataValues[0].stacktrace) {
  9. return {
  10. ...exceptionData,
  11. values: [
  12. {
  13. ...exceptionDataValues[0],
  14. stacktrace: thread.stacktrace,
  15. rawStacktrace: thread.rawStacktrace,
  16. },
  17. ],
  18. };
  19. }
  20. const exceptionHasAtLeastOneStacktrace = !!exceptionDataValues.find(
  21. exceptionDataValue => exceptionDataValue.stacktrace
  22. );
  23. if (!!exceptionHasAtLeastOneStacktrace) {
  24. return exceptionData as Required<ExceptionType>;
  25. }
  26. return undefined;
  27. }
  28. function getThreadException(
  29. event: Event,
  30. thread?: Thread
  31. ): Required<ExceptionType> | undefined {
  32. const exceptionEntry = event.entries.find(entry => entry.type === 'exception');
  33. if (!exceptionEntry) {
  34. return undefined;
  35. }
  36. const exceptionData = exceptionEntry.data as ExceptionType;
  37. const exceptionDataValues = exceptionData.values;
  38. if (!exceptionDataValues?.length || !thread) {
  39. return undefined;
  40. }
  41. const matchedStacktraceAndExceptionThread = exceptionDataValues.find(
  42. exceptionDataValue => exceptionDataValue.threadId === thread.id
  43. );
  44. if (matchedStacktraceAndExceptionThread) {
  45. return getException(exceptionData, exceptionDataValues, thread);
  46. }
  47. if (
  48. exceptionDataValues.every(
  49. exceptionDataValue => !defined(exceptionDataValue.threadId)
  50. ) &&
  51. thread.crashed
  52. ) {
  53. return getException(exceptionData, exceptionDataValues, thread);
  54. }
  55. return undefined;
  56. }
  57. export default getThreadException;