rawContent.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import {Component, Fragment} from 'react';
  2. import styled from '@emotion/styled';
  3. import {Client} from 'sentry/api';
  4. import {Button} from 'sentry/components/button';
  5. import ClippedBox from 'sentry/components/clippedBox';
  6. import LoadingError from 'sentry/components/loadingError';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import {t} from 'sentry/locale';
  9. import {ExceptionType, Organization, PlatformKey, Project} from 'sentry/types';
  10. import {Event} from 'sentry/types/event';
  11. import withApi from 'sentry/utils/withApi';
  12. import withOrganization from 'sentry/utils/withOrganization';
  13. import rawStacktraceContent from '../stackTrace/rawContent';
  14. type Props = {
  15. api: Client;
  16. eventId: Event['id'];
  17. platform: PlatformKey;
  18. projectSlug: Project['slug'];
  19. type: 'original' | 'minified';
  20. // XXX: Organization is NOT available for Shared Issues!
  21. organization?: Organization;
  22. } & Pick<ExceptionType, 'values'>;
  23. type State = {
  24. crashReport: string;
  25. error: boolean;
  26. loading: boolean;
  27. };
  28. class RawContent extends Component<Props, State> {
  29. state: State = {
  30. loading: false,
  31. error: false,
  32. crashReport: '',
  33. };
  34. componentDidMount() {
  35. if (this.isNative()) {
  36. this.fetchAppleCrashReport();
  37. }
  38. }
  39. componentDidUpdate(prevProps: Props) {
  40. if (this.isNative() && this.props.type !== prevProps.type) {
  41. this.fetchAppleCrashReport();
  42. }
  43. }
  44. isNative() {
  45. const {platform} = this.props;
  46. return platform === 'cocoa' || platform === 'native';
  47. }
  48. getAppleCrashReportEndpoint(organization: Organization) {
  49. const {type, projectSlug, eventId} = this.props;
  50. const minified = type === 'minified';
  51. return `/projects/${organization.slug}/${projectSlug}/events/${eventId}/apple-crash-report?minified=${minified}`;
  52. }
  53. getContent(isNative: boolean, exc: any) {
  54. const {type} = this.props;
  55. const output = {
  56. downloadButton: null,
  57. content: exc.stacktrace
  58. ? rawStacktraceContent(
  59. type === 'original' ? exc.stacktrace : exc.rawStacktrace,
  60. this.props.platform,
  61. exc
  62. )
  63. : null,
  64. };
  65. if (!isNative) {
  66. return output;
  67. }
  68. const {loading, error, crashReport} = this.state;
  69. if (loading) {
  70. return {
  71. ...output,
  72. content: <LoadingIndicator />,
  73. };
  74. }
  75. if (error) {
  76. return {
  77. ...output,
  78. content: <LoadingError />,
  79. };
  80. }
  81. if (!loading && !!crashReport) {
  82. const {api, organization} = this.props;
  83. let downloadButton: React.ReactElement | null = null;
  84. if (organization) {
  85. const appleCrashReportEndpoint = this.getAppleCrashReportEndpoint(organization);
  86. downloadButton = (
  87. <DownloadBtnWrapper>
  88. <Button
  89. size="xs"
  90. href={`${api.baseUrl}${appleCrashReportEndpoint}&download=1`}
  91. >
  92. {t('Download')}
  93. </Button>
  94. </DownloadBtnWrapper>
  95. );
  96. }
  97. return {
  98. downloadButton,
  99. content: <ClippedBox clipHeight={250}>{crashReport}</ClippedBox>,
  100. };
  101. }
  102. return output;
  103. }
  104. async fetchAppleCrashReport() {
  105. const {api, organization} = this.props;
  106. // Shared issues do not have access to organization
  107. if (!organization) {
  108. return;
  109. }
  110. this.setState({
  111. loading: true,
  112. error: false,
  113. crashReport: '',
  114. });
  115. try {
  116. const data = await api.requestPromise(
  117. this.getAppleCrashReportEndpoint(organization)
  118. );
  119. this.setState({
  120. error: false,
  121. loading: false,
  122. crashReport: data,
  123. });
  124. } catch {
  125. this.setState({error: true, loading: false});
  126. }
  127. }
  128. render() {
  129. const {values} = this.props;
  130. const isNative = this.isNative();
  131. if (!values) {
  132. return null;
  133. }
  134. return (
  135. <Fragment>
  136. {values.map((exc, excIdx) => {
  137. const {downloadButton, content} = this.getContent(isNative, exc);
  138. if (!downloadButton && !content) {
  139. return null;
  140. }
  141. return (
  142. <div key={excIdx} data-test-id="raw-stack-trace">
  143. <pre className="traceback plain">{content}</pre>
  144. </div>
  145. );
  146. })}
  147. </Fragment>
  148. );
  149. }
  150. }
  151. export default withApi(withOrganization(RawContent));
  152. const DownloadBtnWrapper = styled('div')`
  153. display: flex;
  154. align-items: center;
  155. justify-content: flex-end;
  156. `;