eventEntries.tsx 16 KB

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