eventEntries.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. import {memo, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {Location} from 'history';
  5. import uniq from 'lodash/uniq';
  6. import {addErrorMessage} from 'sentry/actionCreators/indicator';
  7. import {Client} from 'sentry/api';
  8. import ErrorBoundary from 'sentry/components/errorBoundary';
  9. import EventContexts from 'sentry/components/events/contexts';
  10. import EventContextSummary from 'sentry/components/events/contextSummary/contextSummary';
  11. import EventDevice from 'sentry/components/events/device';
  12. import EventErrors, {Error} from 'sentry/components/events/errors';
  13. import EventAttachments from 'sentry/components/events/eventAttachments';
  14. import EventCause from 'sentry/components/events/eventCause';
  15. import EventCauseEmpty from 'sentry/components/events/eventCauseEmpty';
  16. import EventDataSection from 'sentry/components/events/eventDataSection';
  17. import EventExtraData from 'sentry/components/events/eventExtraData/eventExtraData';
  18. import EventSdk from 'sentry/components/events/eventSdk';
  19. import EventTags from 'sentry/components/events/eventTags/eventTags';
  20. import EventGroupingInfo from 'sentry/components/events/groupingInfo';
  21. import EventPackageData from 'sentry/components/events/packageData';
  22. import RRWebIntegration from 'sentry/components/events/rrwebIntegration';
  23. import EventSdkUpdates from 'sentry/components/events/sdkUpdates';
  24. import {DataSection} from 'sentry/components/events/styles';
  25. import EventUserFeedback from 'sentry/components/events/userFeedback';
  26. import ExternalLink from 'sentry/components/links/externalLink';
  27. import {t, tct} from 'sentry/locale';
  28. import space from 'sentry/styles/space';
  29. import {
  30. Entry,
  31. EntryType,
  32. Event,
  33. ExceptionValue,
  34. Group,
  35. IssueAttachment,
  36. Organization,
  37. Project,
  38. SharedViewOrganization,
  39. Thread,
  40. } from 'sentry/types';
  41. import {DebugFile} from 'sentry/types/debugFiles';
  42. import {Image} from 'sentry/types/debugImage';
  43. import {isNotSharedOrganization} from 'sentry/types/utils';
  44. import {defined, objectIsEmpty} from 'sentry/utils';
  45. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  46. import withApi from 'sentry/utils/withApi';
  47. import withOrganization from 'sentry/utils/withOrganization';
  48. import {projectProcessingIssuesMessages} from 'sentry/views/settings/project/projectProcessingIssues';
  49. import findBestThread from './interfaces/threads/threadSelector/findBestThread';
  50. import getThreadException from './interfaces/threads/threadSelector/getThreadException';
  51. import EventEntry from './eventEntry';
  52. import EventTagsAndScreenshot from './eventTagsAndScreenshot';
  53. const MINIFIED_DATA_JAVA_EVENT_REGEX_MATCH =
  54. /^(([\w\$]\.[\w\$]{1,2})|([\w\$]{2}\.[\w\$]\.[\w\$]))(\.|$)/g;
  55. type ProGuardErrors = Array<Error>;
  56. type Props = Pick<React.ComponentProps<typeof EventEntry>, 'route' | 'router'> & {
  57. /**
  58. * The organization can be the shared view on a public issue view.
  59. */
  60. organization: Organization | SharedViewOrganization;
  61. project: Project;
  62. location: Location;
  63. api: Client;
  64. event?: Event;
  65. group?: Group;
  66. isShare?: boolean;
  67. showExampleCommit?: boolean;
  68. showTagSummary?: boolean;
  69. isBorderless?: boolean;
  70. className?: string;
  71. };
  72. const EventEntries = memo(
  73. ({
  74. organization,
  75. project,
  76. location,
  77. api,
  78. event,
  79. group,
  80. className,
  81. router,
  82. route,
  83. isShare = false,
  84. showExampleCommit = false,
  85. showTagSummary = true,
  86. isBorderless = false,
  87. }: Props) => {
  88. const [isLoading, setIsLoading] = useState(true);
  89. const [proGuardErrors, setProGuardErrors] = useState<ProGuardErrors>([]);
  90. const [attachments, setAttachments] = useState<IssueAttachment[]>([]);
  91. const orgSlug = organization.slug;
  92. const projectSlug = project.slug;
  93. const orgFeatures = organization?.features ?? [];
  94. const hasEventAttachmentsFeature = orgFeatures.includes('event-attachments');
  95. useEffect(() => {
  96. checkProGuardError();
  97. recordIssueError();
  98. fetchAttachments();
  99. }, []);
  100. function recordIssueError() {
  101. if (!event || !event.errors || !(event.errors.length > 0)) {
  102. return;
  103. }
  104. const errors = event.errors;
  105. const errorTypes = errors.map(errorEntries => errorEntries.type);
  106. const errorMessages = errors.map(errorEntries => errorEntries.message);
  107. const platform = project.platform;
  108. // uniquify the array types
  109. trackAdvancedAnalyticsEvent('issue_error_banner.viewed', {
  110. organization: organization as Organization,
  111. group: event?.groupID,
  112. error_type: uniq(errorTypes),
  113. error_message: uniq(errorMessages),
  114. ...(platform && {platform}),
  115. });
  116. }
  117. async function fetchProguardMappingFiles(query: string): Promise<Array<DebugFile>> {
  118. try {
  119. const proguardMappingFiles = await api.requestPromise(
  120. `/projects/${orgSlug}/${projectSlug}/files/dsyms/`,
  121. {
  122. method: 'GET',
  123. query: {
  124. query,
  125. file_formats: 'proguard',
  126. },
  127. }
  128. );
  129. return proguardMappingFiles;
  130. } catch (error) {
  131. Sentry.captureException(error);
  132. // do nothing, the UI will not display extra error details
  133. return [];
  134. }
  135. }
  136. function isDataMinified(str: string | null) {
  137. if (!str) {
  138. return false;
  139. }
  140. return !![...str.matchAll(MINIFIED_DATA_JAVA_EVENT_REGEX_MATCH)].length;
  141. }
  142. function hasThreadOrExceptionMinifiedFrameData(
  143. definedEvent: Event,
  144. bestThread?: Thread
  145. ) {
  146. if (!bestThread) {
  147. const exceptionValues: Array<ExceptionValue> =
  148. definedEvent.entries?.find(e => e.type === EntryType.EXCEPTION)?.data?.values ??
  149. [];
  150. return !!exceptionValues.find(exceptionValue =>
  151. exceptionValue.stacktrace?.frames?.find(frame => isDataMinified(frame.module))
  152. );
  153. }
  154. const threadExceptionValues = getThreadException(definedEvent, bestThread)?.values;
  155. return !!(threadExceptionValues
  156. ? threadExceptionValues.find(threadExceptionValue =>
  157. threadExceptionValue.stacktrace?.frames?.find(frame =>
  158. isDataMinified(frame.module)
  159. )
  160. )
  161. : bestThread?.stacktrace?.frames?.find(frame => isDataMinified(frame.module)));
  162. }
  163. async function checkProGuardError() {
  164. if (!event || event.platform !== 'java') {
  165. setIsLoading(false);
  166. return;
  167. }
  168. const hasEventErrorsProGuardMissingMapping = event.errors?.find(
  169. error => error.type === 'proguard_missing_mapping'
  170. );
  171. if (hasEventErrorsProGuardMissingMapping) {
  172. setIsLoading(false);
  173. return;
  174. }
  175. const newProGuardErrors: ProGuardErrors = [];
  176. const debugImages = event.entries?.find(e => e.type === EntryType.DEBUGMETA)?.data
  177. .images as undefined | Array<Image>;
  178. // When debugImages contains a 'proguard' entry, it must always be only one entry
  179. const proGuardImage = debugImages?.find(
  180. debugImage => debugImage?.type === 'proguard'
  181. );
  182. const proGuardImageUuid = proGuardImage?.uuid;
  183. // If an entry is of type 'proguard' and has 'uuid',
  184. // it means that the Sentry Gradle plugin has been executed,
  185. // otherwise the proguard id wouldn't be in the event.
  186. // But maybe it failed to upload the mappings file
  187. if (defined(proGuardImageUuid)) {
  188. if (isShare) {
  189. setIsLoading(false);
  190. return;
  191. }
  192. const proguardMappingFiles = await fetchProguardMappingFiles(proGuardImageUuid);
  193. if (!proguardMappingFiles.length) {
  194. newProGuardErrors.push({
  195. type: 'proguard_missing_mapping',
  196. message: projectProcessingIssuesMessages.proguard_missing_mapping,
  197. data: {mapping_uuid: proGuardImageUuid},
  198. });
  199. }
  200. setProGuardErrors(newProGuardErrors);
  201. setIsLoading(false);
  202. return;
  203. }
  204. if (proGuardImage) {
  205. Sentry.withScope(function (s) {
  206. s.setLevel(Sentry.Severity.Warning);
  207. if (event.sdk) {
  208. s.setTag('offending.event.sdk.name', event.sdk.name);
  209. s.setTag('offending.event.sdk.version', event.sdk.version);
  210. }
  211. Sentry.captureMessage('Event contains proguard image but not uuid');
  212. });
  213. }
  214. const threads: Array<Thread> =
  215. event.entries?.find(e => e.type === EntryType.THREADS)?.data?.values ?? [];
  216. const bestThread = findBestThread(threads);
  217. const hasThreadOrExceptionMinifiedData = hasThreadOrExceptionMinifiedFrameData(
  218. event,
  219. bestThread
  220. );
  221. if (hasThreadOrExceptionMinifiedData) {
  222. newProGuardErrors.push({
  223. type: 'proguard_potentially_misconfigured_plugin',
  224. message: tct(
  225. 'Some frames appear to be minified. Did you configure the [plugin]?',
  226. {
  227. plugin: (
  228. <ExternalLink href="https://docs.sentry.io/platforms/android/proguard/#gradle">
  229. Sentry Gradle Plugin
  230. </ExternalLink>
  231. ),
  232. }
  233. ),
  234. });
  235. }
  236. setProGuardErrors(newProGuardErrors);
  237. setIsLoading(false);
  238. }
  239. async function fetchAttachments() {
  240. if (!event || isShare || !hasEventAttachmentsFeature) {
  241. return;
  242. }
  243. try {
  244. const response = await api.requestPromise(
  245. `/projects/${orgSlug}/${projectSlug}/events/${event.id}/attachments/`
  246. );
  247. setAttachments(response);
  248. } catch (error) {
  249. Sentry.captureException(error);
  250. addErrorMessage('An error occurred while fetching attachments');
  251. }
  252. }
  253. function renderEntries(definedEvent: Event) {
  254. const entries = definedEvent.entries;
  255. if (!Array.isArray(entries)) {
  256. return null;
  257. }
  258. return (entries as Array<Entry>).map((entry, entryIdx) => (
  259. <ErrorBoundary
  260. key={`entry-${entryIdx}`}
  261. customComponent={
  262. <EventDataSection type={entry.type} title={entry.type}>
  263. <p>{t('There was an error rendering this data.')}</p>
  264. </EventDataSection>
  265. }
  266. >
  267. <EventEntry
  268. projectSlug={projectSlug}
  269. group={group}
  270. organization={organization}
  271. event={definedEvent}
  272. entry={entry}
  273. route={route}
  274. router={router}
  275. />
  276. </ErrorBoundary>
  277. ));
  278. }
  279. async function handleDeleteAttachment(attachmentId: IssueAttachment['id']) {
  280. if (!event) {
  281. return;
  282. }
  283. try {
  284. await api.requestPromise(
  285. `/projects/${orgSlug}/${projectSlug}/events/${event.id}/attachments/${attachmentId}/`,
  286. {
  287. method: 'DELETE',
  288. }
  289. );
  290. setAttachments(attachments.filter(attachment => attachment.id !== attachmentId));
  291. } catch (error) {
  292. Sentry.captureException(error);
  293. addErrorMessage('An error occurred while deleteting the attachment');
  294. }
  295. }
  296. if (!event) {
  297. return (
  298. <LatestEventNotAvailable>
  299. <h3>{t('Latest Event Not Available')}</h3>
  300. </LatestEventNotAvailable>
  301. );
  302. }
  303. const hasMobileScreenshotsFeature = orgFeatures.includes('mobile-screenshots');
  304. const hasContext = !objectIsEmpty(event.user ?? {}) || !objectIsEmpty(event.contexts);
  305. const hasErrors = !objectIsEmpty(event.errors) || !!proGuardErrors.length;
  306. return (
  307. <div className={className} data-test-id={`event-entries-loading-${isLoading}`}>
  308. {hasErrors && !isLoading && (
  309. <EventErrors
  310. event={event}
  311. orgSlug={orgSlug}
  312. projectSlug={projectSlug}
  313. proGuardErrors={proGuardErrors}
  314. />
  315. )}
  316. {!isShare &&
  317. isNotSharedOrganization(organization) &&
  318. (showExampleCommit ? (
  319. <EventCauseEmpty
  320. event={event}
  321. organization={organization}
  322. project={project}
  323. />
  324. ) : (
  325. <EventCause
  326. organization={organization}
  327. project={project}
  328. event={event}
  329. group={group}
  330. />
  331. ))}
  332. {event.userReport && group && (
  333. <StyledEventUserFeedback
  334. report={event.userReport}
  335. orgId={orgSlug}
  336. issueId={group.id}
  337. includeBorder={!hasErrors}
  338. />
  339. )}
  340. {showTagSummary &&
  341. (hasMobileScreenshotsFeature ? (
  342. <EventTagsAndScreenshot
  343. event={event}
  344. organization={organization as Organization}
  345. projectId={projectSlug}
  346. location={location}
  347. isShare={isShare}
  348. hasContext={hasContext}
  349. isBorderless={isBorderless}
  350. attachments={attachments}
  351. onDeleteScreenshot={handleDeleteAttachment}
  352. />
  353. ) : (
  354. (!!(event.tags ?? []).length || hasContext) && (
  355. <StyledEventDataSection title={t('Tags')} type="tags">
  356. {hasContext && <EventContextSummary event={event} />}
  357. <EventTags
  358. event={event}
  359. organization={organization as Organization}
  360. projectId={projectSlug}
  361. location={location}
  362. />
  363. </StyledEventDataSection>
  364. )
  365. ))}
  366. {renderEntries(event)}
  367. {hasContext && <EventContexts group={group} event={event} />}
  368. {event && !objectIsEmpty(event.context) && <EventExtraData event={event} />}
  369. {event && !objectIsEmpty(event.packages) && <EventPackageData event={event} />}
  370. {event && !objectIsEmpty(event.device) && <EventDevice event={event} />}
  371. {!isShare && hasEventAttachmentsFeature && (
  372. <EventAttachments
  373. event={event}
  374. orgId={orgSlug}
  375. projectId={projectSlug}
  376. location={location}
  377. attachments={attachments}
  378. onDeleteAttachment={handleDeleteAttachment}
  379. />
  380. )}
  381. {event.sdk && !objectIsEmpty(event.sdk) && <EventSdk sdk={event.sdk} />}
  382. {!isShare && event?.sdkUpdates && event.sdkUpdates.length > 0 && (
  383. <EventSdkUpdates event={{sdkUpdates: event.sdkUpdates, ...event}} />
  384. )}
  385. {!isShare && event.groupID && (
  386. <EventGroupingInfo
  387. projectId={projectSlug}
  388. event={event}
  389. showGroupingConfig={orgFeatures.includes('set-grouping-config')}
  390. />
  391. )}
  392. {!isShare && hasEventAttachmentsFeature && (
  393. <RRWebIntegration event={event} orgId={orgSlug} projectId={projectSlug} />
  394. )}
  395. </div>
  396. );
  397. }
  398. );
  399. const LatestEventNotAvailable = styled('div')`
  400. padding: ${space(2)} ${space(4)};
  401. `;
  402. const ErrorContainer = styled('div')`
  403. /*
  404. Remove border on adjacent context summary box.
  405. Once that component uses emotion this will be harder.
  406. */
  407. & + .context-summary {
  408. border-top: none;
  409. }
  410. `;
  411. const BorderlessEventEntries = styled(EventEntries)`
  412. & ${/* sc-selector */ DataSection} {
  413. padding: ${space(3)} 0 0 0;
  414. }
  415. & ${/* sc-selector */ DataSection}:first-child {
  416. padding-top: 0;
  417. border-top: 0;
  418. }
  419. & ${/* sc-selector */ ErrorContainer} {
  420. margin-bottom: ${space(2)};
  421. }
  422. `;
  423. type StyledEventUserFeedbackProps = {
  424. includeBorder: boolean;
  425. };
  426. const StyledEventUserFeedback = styled(EventUserFeedback)<StyledEventUserFeedbackProps>`
  427. border-radius: 0;
  428. box-shadow: none;
  429. padding: ${space(3)} ${space(4)} 0 40px;
  430. border: 0;
  431. ${p => (p.includeBorder ? `border-top: 1px solid ${p.theme.innerBorder};` : '')}
  432. margin: 0;
  433. `;
  434. const StyledEventDataSection = styled(EventDataSection)`
  435. margin-bottom: ${space(2)};
  436. `;
  437. // TODO(ts): any required due to our use of SharedViewOrganization
  438. export default withOrganization<any>(withApi(EventEntries));
  439. export {BorderlessEventEntries};