capacitor.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  2. import type {
  3. Docs,
  4. DocsParams,
  5. OnboardingConfig,
  6. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  7. import {t, tct} from 'sentry/locale';
  8. export enum CapacitorVersion {
  9. V2 = 'v2',
  10. V3 = 'v3',
  11. }
  12. const platformOptions = {
  13. capacitorVersion: {
  14. label: t('Capacitor Version'),
  15. items: [
  16. {
  17. label: t('Capacitor 3'),
  18. value: CapacitorVersion.V3,
  19. },
  20. {
  21. label: t('Capacitor 2'),
  22. value: CapacitorVersion.V2,
  23. },
  24. ],
  25. },
  26. };
  27. type PlatformOptions = typeof platformOptions;
  28. type Params = DocsParams<PlatformOptions>;
  29. const getInstallCapacitor2SnippetJava = () => `
  30. import android.os.Bundle;
  31. import com.getcapacitor.BridgeActivity;
  32. import com.getcapacitor.Plugin;
  33. import io.sentry.capacitor.SentryCapacitor;
  34. import java.util.ArrayList;
  35. public class MainActivity extends BridgeActivity {
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. // Initializes the Bridge
  40. this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
  41. add(SentryCapacitor.class);
  42. }});
  43. }
  44. }`;
  45. const getInstallCapacitor2SnippetKotlin = () => `
  46. import android.os.Bundle
  47. import com.getcapacitor.BridgeActivity
  48. import com.getcapacitor.Plugin
  49. import io.sentry.capacitor.SentryCapacitor
  50. class MainActivity : BridgeActivity() {
  51. override fun onCreate(savedInstanceState: Bundle?) {
  52. super.onCreate(savedInstanceState)
  53. // Initializes the Bridge
  54. this.init(
  55. savedInstanceState,
  56. listOf<Class<out Plugin>>(SentryCapacitor::class.java)
  57. )
  58. }
  59. }`;
  60. const getConfigureSnippetAngular = (params: Params) => `
  61. // app.module.ts
  62. import * as Sentry from '@sentry/capacitor';
  63. // Use "@sentry/angular-ivy" for Angular 12+ or "@sentry/angular" for Angular 10 or 11
  64. import * as SentryAngular from '@sentry/angular-ivy';
  65. Sentry.init(
  66. {
  67. dsn: '${params.dsn}',
  68. // To set your release and dist versions
  69. release: 'my-project-name@' + process.env.npm_package_version,
  70. dist: '1',
  71. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  72. // We recommend adjusting this value in production.
  73. tracesSampleRate: 1.0,
  74. integrations: [
  75. new SentryAngular.BrowserTracing({
  76. // Set "tracePropagationTargets" to control for which URLs distributed tracing should be enabled
  77. tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],
  78. routingInstrumentation: SentryAngular.routingInstrumentation,
  79. }),
  80. ]
  81. },
  82. // Forward the init method from @sentry/angular
  83. SentryAngular.init
  84. );
  85. @NgModule({
  86. providers: [
  87. {
  88. provide: ErrorHandler,
  89. // Attach the Sentry ErrorHandler
  90. useValue: SentryAngular.createErrorHandler(),
  91. },
  92. {
  93. provide: SentryAngular.TraceService,
  94. deps: [Router],
  95. },
  96. {
  97. provide: APP_INITIALIZER,
  98. useFactory: () => () => {},
  99. deps: [SentryAngular.TraceService],
  100. multi: true,
  101. },
  102. ],
  103. })`;
  104. const getConfigureSnippetStandalone = (params: Params) => `
  105. // App.js
  106. import * as Sentry from "@sentry/capacitor";
  107. Sentry.init({
  108. dsn: "${params.dsn}",
  109. // Set your release version, such as 'getsentry@1.0.0'
  110. release: "my-project-name@<release-name>",
  111. // Set your dist version, such as "1"
  112. dist: "<dist>",
  113. });`;
  114. const onboarding: OnboardingConfig<PlatformOptions> = {
  115. install: params => [
  116. {
  117. type: StepType.INSTALL,
  118. description: t(
  119. 'Install the Sentry Capacitor SDK alongside the sibling Sentry Angular SDK:'
  120. ),
  121. configurations: [
  122. {
  123. language: 'bash',
  124. code: [
  125. {
  126. label: 'npm',
  127. value: 'npm',
  128. language: 'bash',
  129. code: 'npm install --save @sentry/capacitor @sentry/angular-ivy',
  130. },
  131. {
  132. label: 'yarn',
  133. value: 'yarn',
  134. language: 'bash',
  135. code: 'yarn add @sentry/capacitor @sentry/angular @sentry/tracing --exact',
  136. },
  137. ],
  138. },
  139. {
  140. language: 'bash',
  141. description: t(
  142. "Or install the standalone Sentry Capacitor SDK if you don't use Ionic/Angular:"
  143. ),
  144. code: [
  145. {
  146. label: 'npm',
  147. value: 'npm',
  148. language: 'bash',
  149. code: 'npm install --save @sentry/capacitor @sentry/tracing',
  150. },
  151. {
  152. label: 'yarn',
  153. value: 'yarn',
  154. language: 'bash',
  155. code: 'yarn add @sentry/capacitor',
  156. },
  157. ],
  158. },
  159. ],
  160. additionalInfo: tct(
  161. 'The version of the sibling SDK must match with the version referred by Sentry Capacitor. To check which version of the sibling SDK is installed, use the following command: [code:npm info @sentry/capacitor peerDependencies]',
  162. {code: <code />}
  163. ),
  164. },
  165. ...(params.platformOptions.capacitorVersion === CapacitorVersion.V2
  166. ? [
  167. {
  168. title: t('Capacitor 2 - Android'),
  169. description: tct(
  170. 'Add the [sentryCapacitorCode:SentryCapacitor] plugin class inside the [onCreateCode:onCreate] method of your [mainActivityCode:MainActivity] file.',
  171. {
  172. sentryCapacitorCode: <code />,
  173. onCreateCode: <code />,
  174. mainActivityCode: <code />,
  175. }
  176. ),
  177. configurations: [
  178. {
  179. language: 'java',
  180. code: [
  181. {
  182. label: 'Java',
  183. value: 'java',
  184. language: 'java',
  185. code: getInstallCapacitor2SnippetJava(),
  186. },
  187. {
  188. label: 'Kotlin',
  189. value: 'kotlin',
  190. language: 'kotlin',
  191. code: getInstallCapacitor2SnippetKotlin(),
  192. },
  193. ],
  194. },
  195. ],
  196. },
  197. ]
  198. : []),
  199. ],
  200. configure: params => [
  201. {
  202. type: StepType.CONFIGURE,
  203. description: t('You must initialize the Sentry SDK as early as you can:'),
  204. configurations: [
  205. {
  206. description: t('With Ionic/Angular:'),
  207. language: 'typescript',
  208. code: getConfigureSnippetAngular(params),
  209. },
  210. {
  211. description: t('Standalone:'),
  212. language: 'javascript',
  213. code: getConfigureSnippetStandalone(params),
  214. },
  215. ],
  216. },
  217. ],
  218. verify: () => [
  219. {
  220. type: StepType.VERIFY,
  221. description: t(
  222. 'This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:'
  223. ),
  224. configurations: [
  225. {
  226. language: 'javascript',
  227. code: `
  228. import * as Sentry from "@sentry/capacitor";
  229. Sentry.captureException("Test Captured Exception");
  230. `,
  231. },
  232. {
  233. language: 'javascript',
  234. description: t('You can also throw an error anywhere in your application:'),
  235. code: `
  236. // Must be thrown after Sentry.init is called to be captured.
  237. throw new Error("Test Thrown Error");
  238. `,
  239. },
  240. {
  241. language: 'javascript',
  242. description: t('Or trigger a native crash:'),
  243. code: `
  244. import * as Sentry from "@sentry/capacitor";
  245. Sentry.nativeCrash();
  246. `,
  247. },
  248. ],
  249. },
  250. ],
  251. };
  252. const docs: Docs<PlatformOptions> = {
  253. onboarding,
  254. platformOptions,
  255. };
  256. export default docs;