eventErrors.tsx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. import {useEffect} from 'react';
  2. import styled from '@emotion/styled';
  3. import isEqual from 'lodash/isEqual';
  4. import uniq from 'lodash/uniq';
  5. import uniqWith from 'lodash/uniqWith';
  6. import {Alert} from 'sentry/components/alert';
  7. import {ErrorItem, EventErrorData} from 'sentry/components/events/errorItem';
  8. import findBestThread from 'sentry/components/events/interfaces/threads/threadSelector/findBestThread';
  9. import getThreadException from 'sentry/components/events/interfaces/threads/threadSelector/getThreadException';
  10. import ExternalLink from 'sentry/components/links/externalLink';
  11. import List from 'sentry/components/list';
  12. import {JavascriptProcessingErrors} from 'sentry/constants/eventErrors';
  13. import {tct, tn} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import {Project} from 'sentry/types';
  16. import {DebugFile} from 'sentry/types/debugFiles';
  17. import {Image} from 'sentry/types/debugImage';
  18. import {EntryType, Event, ExceptionValue, Thread} from 'sentry/types/event';
  19. import {defined} from 'sentry/utils';
  20. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  21. import {useQuery} from 'sentry/utils/queryClient';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import {projectProcessingIssuesMessages} from 'sentry/views/settings/project/projectProcessingIssues';
  24. import {DataSection} from './styles';
  25. const ERRORS_TO_HIDE = [JavascriptProcessingErrors.JS_MISSING_SOURCE];
  26. const MAX_ERRORS = 100;
  27. const MINIFIED_DATA_JAVA_EVENT_REGEX_MATCH =
  28. /^(([\w\$]\.[\w\$]{1,2})|([\w\$]{2}\.[\w\$]\.[\w\$]))(\.|$)/g;
  29. type EventErrorsProps = {
  30. event: Event;
  31. isShare: boolean;
  32. project: Project;
  33. };
  34. function isDataMinified(str: string | null) {
  35. if (!str) {
  36. return false;
  37. }
  38. return !![...str.matchAll(MINIFIED_DATA_JAVA_EVENT_REGEX_MATCH)].length;
  39. }
  40. const hasThreadOrExceptionMinifiedFrameData = (
  41. definedEvent: Event,
  42. bestThread?: Thread
  43. ) => {
  44. if (!bestThread) {
  45. const exceptionValues: Array<ExceptionValue> =
  46. definedEvent.entries?.find(e => e.type === EntryType.EXCEPTION)?.data?.values ?? [];
  47. return !!exceptionValues.find(exceptionValue =>
  48. exceptionValue.stacktrace?.frames?.find(frame => isDataMinified(frame.module))
  49. );
  50. }
  51. const threadExceptionValues = getThreadException(definedEvent, bestThread)?.values;
  52. return !!(threadExceptionValues
  53. ? threadExceptionValues.find(threadExceptionValue =>
  54. threadExceptionValue.stacktrace?.frames?.find(frame =>
  55. isDataMinified(frame.module)
  56. )
  57. )
  58. : bestThread?.stacktrace?.frames?.find(frame => isDataMinified(frame.module)));
  59. };
  60. const useFetchProguardMappingFiles = ({
  61. event,
  62. isShare,
  63. project,
  64. }: {
  65. event: Event;
  66. isShare: boolean;
  67. project: Project;
  68. }): {proguardErrors: EventErrorData[]; proguardErrorsLoading: boolean} => {
  69. const organization = useOrganization();
  70. const hasEventErrorsProGuardMissingMapping = event.errors?.find(
  71. error => error.type === 'proguard_missing_mapping'
  72. );
  73. const debugImages = event.entries?.find(e => e.type === EntryType.DEBUGMETA)?.data
  74. .images as undefined | Array<Image>;
  75. // When debugImages contains a 'proguard' entry, it must always be only one entry
  76. const proGuardImage = debugImages?.find(debugImage => debugImage?.type === 'proguard');
  77. const proGuardImageUuid = proGuardImage?.uuid;
  78. const shouldFetch =
  79. defined(proGuardImageUuid) &&
  80. event.platform === 'java' &&
  81. !hasEventErrorsProGuardMissingMapping &&
  82. !isShare;
  83. const {
  84. data: proguardMappingFiles,
  85. isSuccess,
  86. isLoading,
  87. } = useQuery<DebugFile[]>(
  88. [
  89. `/projects/${organization.slug}/${project.slug}/files/dsyms/`,
  90. {
  91. query: {
  92. query: proGuardImageUuid,
  93. file_formats: 'proguard',
  94. },
  95. },
  96. ],
  97. {
  98. staleTime: Infinity,
  99. enabled: shouldFetch,
  100. }
  101. );
  102. const getProguardErrorsFromMappingFiles = () => {
  103. if (isShare) {
  104. return [];
  105. }
  106. if (shouldFetch) {
  107. if (!isSuccess || proguardMappingFiles.length > 0) {
  108. return [];
  109. }
  110. return [
  111. {
  112. type: 'proguard_missing_mapping',
  113. message: projectProcessingIssuesMessages.proguard_missing_mapping,
  114. data: {mapping_uuid: proGuardImageUuid},
  115. },
  116. ];
  117. }
  118. const threads: Array<Thread> =
  119. event.entries?.find(e => e.type === EntryType.THREADS)?.data?.values ?? [];
  120. const bestThread = findBestThread(threads);
  121. const hasThreadOrExceptionMinifiedData = hasThreadOrExceptionMinifiedFrameData(
  122. event,
  123. bestThread
  124. );
  125. if (hasThreadOrExceptionMinifiedData) {
  126. return [
  127. {
  128. type: 'proguard_potentially_misconfigured_plugin',
  129. message: tct(
  130. 'Some frames appear to be minified. Did you configure the [plugin]?',
  131. {
  132. plugin: (
  133. <ExternalLink
  134. href="https://docs.sentry.io/platforms/android/proguard/#gradle"
  135. onClick={() => {
  136. trackAdvancedAnalyticsEvent(
  137. 'issue_error_banner.proguard_misconfigured.clicked',
  138. {
  139. organization,
  140. group: event?.groupID,
  141. }
  142. );
  143. }}
  144. >
  145. Sentry Gradle Plugin
  146. </ExternalLink>
  147. ),
  148. }
  149. ),
  150. },
  151. ];
  152. }
  153. return [];
  154. };
  155. return {
  156. proguardErrorsLoading: shouldFetch && isLoading,
  157. proguardErrors: getProguardErrorsFromMappingFiles(),
  158. };
  159. };
  160. const useRecordAnalyticsEvent = ({event, project}: {event: Event; project: Project}) => {
  161. const organization = useOrganization();
  162. useEffect(() => {
  163. if (!event || !event.errors || !(event.errors.length > 0)) {
  164. return;
  165. }
  166. const errors = event.errors;
  167. const errorTypes = errors.map(errorEntries => errorEntries.type);
  168. const errorMessages = errors.map(errorEntries => errorEntries.message);
  169. const platform = project.platform;
  170. // uniquify the array types
  171. trackAdvancedAnalyticsEvent('issue_error_banner.viewed', {
  172. organization,
  173. group: event?.groupID,
  174. error_type: uniq(errorTypes),
  175. error_message: uniq(errorMessages),
  176. ...(platform && {platform}),
  177. });
  178. }, [event, organization, project.platform]);
  179. };
  180. export const EventErrors = ({event, project, isShare}: EventErrorsProps) => {
  181. const organization = useOrganization();
  182. useRecordAnalyticsEvent({event, project});
  183. const {proguardErrorsLoading, proguardErrors} = useFetchProguardMappingFiles({
  184. event,
  185. project,
  186. isShare,
  187. });
  188. useEffect(() => {
  189. if (proguardErrors?.length) {
  190. if (proguardErrors[0]?.type === 'proguard_potentially_misconfigured_plugin') {
  191. trackAdvancedAnalyticsEvent(
  192. 'issue_error_banner.proguard_misconfigured.displayed',
  193. {
  194. organization,
  195. group: event?.groupID,
  196. platform: project.platform,
  197. }
  198. );
  199. } else if (proguardErrors[0]?.type === 'proguard_missing_mapping') {
  200. trackAdvancedAnalyticsEvent(
  201. 'issue_error_banner.proguard_missing_mapping.displayed',
  202. {
  203. organization,
  204. group: event?.groupID,
  205. platform: project.platform,
  206. }
  207. );
  208. }
  209. }
  210. // Just for analytics, only track this once per visit
  211. // eslint-disable-next-line react-hooks/exhaustive-deps
  212. }, []);
  213. const {errors: eventErrors = [], _meta} = event;
  214. // XXX: uniqWith returns unique errors and is not performant with large datasets
  215. const otherErrors: Array<EventErrorData> =
  216. eventErrors.length > MAX_ERRORS ? eventErrors : uniqWith(eventErrors, isEqual);
  217. const errors = [...otherErrors, ...proguardErrors].filter(
  218. error => !ERRORS_TO_HIDE.includes(error.type as JavascriptProcessingErrors)
  219. );
  220. if (proguardErrorsLoading) {
  221. // XXX: This is necessary for acceptance tests to wait until removal since there is
  222. // no visual loading state.
  223. return <HiddenDiv data-test-id="event-errors-loading" />;
  224. }
  225. if (errors.length === 0) {
  226. return null;
  227. }
  228. return (
  229. <StyledDataSection>
  230. <StyledAlert
  231. type="error"
  232. showIcon
  233. data-test-id="event-error-alert"
  234. expand={
  235. <ErrorList data-test-id="event-error-details" symbol="bullet">
  236. {errors.map((error, errorIdx) => {
  237. const data = error.data ?? {};
  238. const meta = _meta?.errors?.[errorIdx];
  239. return <ErrorItem key={errorIdx} error={{...error, data}} meta={meta} />;
  240. })}
  241. </ErrorList>
  242. }
  243. >
  244. {tn(
  245. 'There was %s problem processing this event',
  246. 'There were %s problems processing this event',
  247. errors.length
  248. )}
  249. </StyledAlert>
  250. </StyledDataSection>
  251. );
  252. };
  253. const HiddenDiv = styled('div')`
  254. display: none;
  255. `;
  256. const StyledDataSection = styled(DataSection)`
  257. border-top: none;
  258. @media (min-width: ${p => p.theme.breakpoints.small}) {
  259. padding-top: 0;
  260. }
  261. `;
  262. const StyledAlert = styled(Alert)`
  263. margin: ${space(0.5)} 0;
  264. `;
  265. const ErrorList = styled(List)`
  266. li:last-child {
  267. margin-bottom: 0;
  268. }
  269. `;