ionic.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import {Fragment} from 'react';
  2. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  3. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import {t, tct} from 'sentry/locale';
  6. // Configuration Start
  7. export const steps = ({
  8. dsn,
  9. }: {
  10. dsn?: string;
  11. } = {}): LayoutProps['steps'] => [
  12. {
  13. type: StepType.INSTALL,
  14. description: (
  15. <p>
  16. {tct(
  17. "To use Sentry in your Ionic app, install the Sentry Capacitor SDK alongside the sibling Sentry SDK related to the Web framework you're using with Ionic. The supported siblings are: Angular [sentryAngularIvyCode:@sentry/angular-ivy], React [sentryReactCode:@sentry/react] and Vue [sentryVueCode:@sentry/vue].",
  18. {
  19. sentryAngularIvyCode: <code />,
  20. sentryReactCode: <code />,
  21. sentryVueCode: <code />,
  22. }
  23. )}
  24. </p>
  25. ),
  26. configurations: [
  27. {
  28. language: 'bash',
  29. description: t(
  30. 'Heres an example of installing Sentry Capacitor along with Sentry Angular:'
  31. ),
  32. code: 'npm install --save @sentry/capacitor @sentry/angular',
  33. },
  34. {
  35. language: 'bash',
  36. description: t('or'),
  37. code: 'yarn add @sentry/capacitor @sentry/angular',
  38. },
  39. {
  40. description: (
  41. <Fragment>
  42. <h5>{t('Capacitor 2 - Android')}</h5>
  43. {t('This step is not needed if you are using Capacitor 3')}
  44. <p>
  45. {tct(
  46. 'Then, add the [sentryCapacitorCode:SentryCapacitor] plugin class inside the [onCreateCode:onCreate] method of your [mainActivityCode:MainActivity] file.',
  47. {
  48. sentryCapacitorCode: <code />,
  49. onCreateCode: <code />,
  50. mainActivityCode: <code />,
  51. }
  52. )}
  53. </p>
  54. </Fragment>
  55. ),
  56. configurations: [
  57. {
  58. description: <strong>Java</strong>,
  59. language: 'java',
  60. code: `
  61. import android.os.Bundle;
  62. import com.getcapacitor.BridgeActivity;
  63. import com.getcapacitor.Plugin;
  64. import io.sentry.capacitor.SentryCapacitor;
  65. import java.util.ArrayList;
  66. public class MainActivity extends BridgeActivity {
  67. @Override
  68. public void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. // Initializes the Bridge
  71. this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
  72. add(SentryCapacitor.class);
  73. }});
  74. }
  75. }
  76. `,
  77. },
  78. {
  79. description: <strong>Kotlin</strong>,
  80. language: 'kotlin',
  81. code: `
  82. import android.os.Bundle
  83. import com.getcapacitor.BridgeActivity
  84. import com.getcapacitor.Plugin
  85. import io.sentry.capacitor.SentryCapacitor
  86. class MainActivity : BridgeActivity() {
  87. override fun onCreate(savedInstanceState: Bundle?) {
  88. super.onCreate(savedInstanceState)
  89. // Initializes the Bridge
  90. this.init(
  91. savedInstanceState,
  92. listOf<Class<out Plugin>>(SentryCapacitor::class.java)
  93. )
  94. }
  95. }
  96. `,
  97. },
  98. ],
  99. },
  100. ],
  101. additionalInfo: (
  102. <p>
  103. {tct(
  104. 'The same installation process applies to the other siblings, all you need to do is to replace [code:@sentry/angular-ivy] by the desired sibling.',
  105. {code: <code />}
  106. )}
  107. </p>
  108. ),
  109. },
  110. {
  111. type: StepType.CONFIGURE,
  112. description: (
  113. <p>
  114. {tct('You must initialize the Sentry SDK as early as you can:', {code: <code />})}
  115. </p>
  116. ),
  117. configurations: [
  118. {
  119. language: 'javascript',
  120. code: `
  121. import * as Sentry from "@sentry/capacitor";
  122. // The example is using Angular 12+. Import '@sentry/angular' for Angular 10 and 11. Import '@sentry/vue' or '@sentry/react' when using a Sibling different than Angular.
  123. import * as SentrySibling from "@sentry/angular-ivy";
  124. Sentry.init(
  125. {
  126. dsn: "${dsn}",
  127. // To set your release and dist versions
  128. release: "my-project-name@" + process.env.npm_package_version,
  129. dist: "1",
  130. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  131. // We recommend adjusting this value in production.
  132. tracesSampleRate: 1.0,
  133. integrations: [
  134. new SentrySibling.BrowserTracing({
  135. // Set "tracePropagationTargets" to control for which URLs distributed tracing should be enabled
  136. tracePropagationTargets: [
  137. "localhost",
  138. /^https:\/\/yourserver\.io\/api/,
  139. ],
  140. routingInstrumentation: SentrySibling.routingInstrumentation,
  141. }),
  142. ],
  143. },
  144. // Forward the init method to the sibling Framework.
  145. SentrySibling.init
  146. );
  147. `,
  148. },
  149. {
  150. language: 'javascript',
  151. description: (
  152. <p>
  153. {tct(
  154. "Additionally for Angular, you will also need to configure your root [code:app.module.ts] (same code doesn't apply to other siblings):",
  155. {
  156. code: <code />,
  157. }
  158. )}
  159. </p>
  160. ),
  161. code: `
  162. @NgModule({
  163. providers: [
  164. {
  165. provide: ErrorHandler,
  166. // Attach the Sentry ErrorHandler
  167. useValue: SentrySibling.createErrorHandler(),
  168. },
  169. {
  170. provide: SentrySibling.TraceService,
  171. deps: [Router],
  172. },
  173. {
  174. provide: APP_INITIALIZER,
  175. useFactory: () => () => {},
  176. deps: [SentrySibling.TraceService],
  177. multi: true,
  178. },
  179. ],
  180. })
  181. `,
  182. },
  183. ],
  184. },
  185. {
  186. type: StepType.VERIFY,
  187. description: t(
  188. 'This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:'
  189. ),
  190. configurations: [
  191. {
  192. language: 'javascript',
  193. code: `
  194. import * as Sentry from "@sentry/capacitor";
  195. Sentry.captureException("Test Captured Exception");
  196. `,
  197. },
  198. {
  199. language: 'javascript',
  200. description: t('You can also throw an error anywhere in your application:'),
  201. code: `
  202. // Must be thrown after Sentry.init is called to be captured.
  203. throw new Error("Test Thrown Error");
  204. `,
  205. },
  206. {
  207. language: 'javascript',
  208. description: t('Or trigger a native crash:'),
  209. code: `
  210. import * as Sentry from "@sentry/capacitor";
  211. Sentry.nativeCrash();
  212. `,
  213. },
  214. ],
  215. },
  216. ];
  217. // Configuration End
  218. export function GettingStartedWithIonic({dsn, ...props}: ModuleProps) {
  219. return <Layout steps={steps({dsn})} {...props} />;
  220. }
  221. export default GettingStartedWithIonic;