eventErrors.tsx 10 KB

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