ionic.tsx 6.5 KB

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