content.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import {useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Button} from 'sentry/components/button';
  4. import {
  5. prepareSourceMapDebuggerFrameInformation,
  6. useSourceMapDebuggerData,
  7. } from 'sentry/components/events/interfaces/crashContent/exception/useSourceMapDebuggerData';
  8. import {AnnotatedText} from 'sentry/components/events/meta/annotatedText';
  9. import {Tooltip} from 'sentry/components/tooltip';
  10. import {tct, tn} from 'sentry/locale';
  11. import {space} from 'sentry/styles/space';
  12. import {ExceptionType, Project} from 'sentry/types';
  13. import {Event, ExceptionValue} from 'sentry/types/event';
  14. import {StackType} from 'sentry/types/stacktrace';
  15. import {defined} from 'sentry/utils';
  16. import {Mechanism} from './mechanism';
  17. import {RelatedExceptions} from './relatedExceptions';
  18. import StackTrace from './stackTrace';
  19. type StackTraceProps = React.ComponentProps<typeof StackTrace>;
  20. type Props = {
  21. event: Event;
  22. platform: StackTraceProps['platform'];
  23. projectSlug: Project['slug'];
  24. type: StackType;
  25. meta?: Record<any, any>;
  26. newestFirst?: boolean;
  27. stackView?: StackTraceProps['stackView'];
  28. threadId?: number;
  29. } & Pick<ExceptionType, 'values'> &
  30. Pick<
  31. React.ComponentProps<typeof StackTrace>,
  32. 'groupingCurrentLevel' | 'hasHierarchicalGrouping'
  33. >;
  34. type CollapsedExceptionMap = {[exceptionId: number]: boolean};
  35. const useCollapsedExceptions = (values?: ExceptionValue[]) => {
  36. const [collapsedExceptions, setCollapsedSections] = useState<CollapsedExceptionMap>(
  37. () => {
  38. if (!values) {
  39. return {};
  40. }
  41. return values
  42. .filter(
  43. ({mechanism}) => mechanism?.is_exception_group && defined(mechanism.parent_id)
  44. )
  45. .reduce(
  46. (acc, next) => ({...acc, [next.mechanism?.exception_id ?? -1]: true}),
  47. {}
  48. );
  49. }
  50. );
  51. const toggleException = (exceptionId: number) => {
  52. setCollapsedSections(old => {
  53. if (!defined(old[exceptionId])) {
  54. return old;
  55. }
  56. return {...old, [exceptionId]: !old[exceptionId]};
  57. });
  58. };
  59. const expandException = (exceptionId: number) => {
  60. setCollapsedSections(old => {
  61. const exceptionValue = values?.find(
  62. value => value.mechanism?.exception_id === exceptionId
  63. );
  64. const exceptionGroupId = exceptionValue?.mechanism?.parent_id;
  65. if (!exceptionGroupId || !defined(old[exceptionGroupId])) {
  66. return old;
  67. }
  68. return {...old, [exceptionGroupId]: false};
  69. });
  70. };
  71. return {toggleException, collapsedExceptions, expandException};
  72. };
  73. function ToggleExceptionButton({
  74. values,
  75. exception,
  76. toggleException,
  77. collapsedExceptions,
  78. }: {
  79. collapsedExceptions: CollapsedExceptionMap;
  80. exception: ExceptionValue;
  81. toggleException: (exceptionId: number) => void;
  82. values: ExceptionValue[];
  83. }) {
  84. const exceptionId = exception.mechanism?.exception_id;
  85. if (!exceptionId || !defined(collapsedExceptions[exceptionId])) {
  86. return null;
  87. }
  88. const collapsed = collapsedExceptions[exceptionId];
  89. const numChildren = values.filter(
  90. ({mechanism}) => mechanism?.parent_id === exceptionId
  91. ).length;
  92. return (
  93. <ShowRelatedExceptionsButton
  94. priority="link"
  95. onClick={() => toggleException(exceptionId)}
  96. >
  97. {collapsed
  98. ? tn('Show %s related exceptions', 'Show %s related exceptions', numChildren)
  99. : tn('Hide %s related exceptions', 'Hide %s related exceptions', numChildren)}
  100. </ShowRelatedExceptionsButton>
  101. );
  102. }
  103. export function Content({
  104. newestFirst,
  105. event,
  106. stackView,
  107. groupingCurrentLevel,
  108. hasHierarchicalGrouping,
  109. platform,
  110. projectSlug,
  111. values,
  112. type,
  113. meta,
  114. threadId,
  115. }: Props) {
  116. const {collapsedExceptions, toggleException, expandException} =
  117. useCollapsedExceptions(values);
  118. const sourceMapDebuggerData = useSourceMapDebuggerData(event, projectSlug);
  119. // Organization context may be unavailable for the shared event view, so we
  120. // avoid using the `useOrganization` hook here and directly useContext
  121. // instead.
  122. if (!values) {
  123. return null;
  124. }
  125. const children = values.map((exc, excIdx) => {
  126. const id = defined(exc.mechanism?.exception_id)
  127. ? `exception-${exc.mechanism?.exception_id}`
  128. : undefined;
  129. const frameSourceMapDebuggerData = sourceMapDebuggerData?.exceptions[
  130. excIdx
  131. ].frames.map(debuggerFrame =>
  132. prepareSourceMapDebuggerFrameInformation(sourceMapDebuggerData, debuggerFrame)
  133. );
  134. if (exc.mechanism?.parent_id && collapsedExceptions[exc.mechanism.parent_id]) {
  135. return null;
  136. }
  137. return (
  138. <div key={excIdx} className="exception" data-test-id="exception-value">
  139. {defined(exc?.module) ? (
  140. <Tooltip title={tct('from [exceptionModule]', {exceptionModule: exc?.module})}>
  141. <Title id={id}>{exc.type}</Title>
  142. </Tooltip>
  143. ) : (
  144. <Title id={id}>{exc.type}</Title>
  145. )}
  146. <StyledPre className="exc-message">
  147. {meta?.[excIdx]?.value?.[''] && !exc.value ? (
  148. <AnnotatedText value={exc.value} meta={meta?.[excIdx]?.value?.['']} />
  149. ) : (
  150. exc.value
  151. )}
  152. </StyledPre>
  153. <ToggleExceptionButton
  154. {...{collapsedExceptions, toggleException, values, exception: exc}}
  155. />
  156. {exc.mechanism && (
  157. <Mechanism data={exc.mechanism} meta={meta?.[excIdx]?.mechanism} />
  158. )}
  159. <RelatedExceptions
  160. mechanism={exc.mechanism}
  161. allExceptions={values}
  162. newestFirst={newestFirst}
  163. onExceptionClick={expandException}
  164. />
  165. <StackTrace
  166. data={
  167. type === StackType.ORIGINAL
  168. ? exc.stacktrace
  169. : exc.rawStacktrace || exc.stacktrace
  170. }
  171. stackView={stackView}
  172. stacktrace={exc.stacktrace}
  173. expandFirstFrame={excIdx === values.length - 1}
  174. platform={platform}
  175. newestFirst={newestFirst}
  176. event={event}
  177. chainedException={values.length > 1}
  178. hasHierarchicalGrouping={hasHierarchicalGrouping}
  179. groupingCurrentLevel={groupingCurrentLevel}
  180. meta={meta?.[excIdx]?.stacktrace}
  181. threadId={threadId}
  182. frameSourceMapDebuggerData={frameSourceMapDebuggerData}
  183. stackType={type}
  184. />
  185. </div>
  186. );
  187. });
  188. if (newestFirst) {
  189. children.reverse();
  190. }
  191. return <div>{children}</div>;
  192. }
  193. const StyledPre = styled('pre')`
  194. margin-bottom: ${space(1)};
  195. margin-top: 0;
  196. `;
  197. const Title = styled('h5')`
  198. margin-bottom: ${space(0.5)};
  199. overflow-wrap: break-word;
  200. word-wrap: break-word;
  201. word-break: break-word;
  202. `;
  203. const ShowRelatedExceptionsButton = styled(Button)`
  204. font-family: ${p => p.theme.text.familyMono};
  205. font-size: ${p => p.theme.fontSizeSmall};
  206. `;