nestjs.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import ExternalLink from 'sentry/components/links/externalLink';
  2. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  3. import type {
  4. Docs,
  5. DocsParams,
  6. OnboardingConfig,
  7. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  8. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  9. import {
  10. getCrashReportApiIntroduction,
  11. getCrashReportInstallDescription,
  12. getCrashReportJavaScriptInstallStep,
  13. getCrashReportModalConfigDescription,
  14. getCrashReportModalIntroduction,
  15. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  16. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  17. import {t, tct} from 'sentry/locale';
  18. import {
  19. getImportInstrumentSnippet,
  20. getInstallConfig,
  21. getSdkInitSnippet,
  22. getSentryImportSnippet,
  23. } from 'sentry/utils/gettingStartedDocs/node';
  24. type Params = DocsParams;
  25. const getSdkSetupSnippet = () => `
  26. ${getImportInstrumentSnippet('esm')}
  27. // All other imports below
  28. ${getSentryImportSnippet('nestjs', 'esm')}
  29. import { BaseExceptionFilter, HttpAdapterHost, NestFactory } from '@nestjs/core';
  30. import { AppModule } from './app.module';
  31. async function bootstrap() {
  32. const app = await NestFactory.create(AppModule);
  33. const { httpAdapter } = app.get(HttpAdapterHost);
  34. Sentry.setupNestErrorHandler(app, new BaseExceptionFilter(httpAdapter));
  35. await app.listen(3000);
  36. }
  37. bootstrap();
  38. `;
  39. const getVerifySnippet = () => `
  40. app.use(async function () {
  41. throw new Error("My first Sentry error!");
  42. });
  43. `;
  44. const onboarding: OnboardingConfig = {
  45. install: params => [
  46. {
  47. type: StepType.INSTALL,
  48. description: t('Add the Sentry NestJS SDK as a dependency:'),
  49. configurations: getInstallConfig(params, {
  50. basePackage: '@sentry/nestjs',
  51. }),
  52. },
  53. ],
  54. configure: params => [
  55. {
  56. type: StepType.CONFIGURE,
  57. description: t(
  58. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  59. ),
  60. configurations: [
  61. {
  62. description: tct(
  63. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  64. {code: <code />}
  65. ),
  66. code: [
  67. {
  68. label: 'JavaScript',
  69. value: 'javascript',
  70. language: 'javascript',
  71. filename: 'instrument.(js|ts)',
  72. code: getSdkInitSnippet(params, 'nestjs', 'esm'),
  73. },
  74. ],
  75. },
  76. {
  77. description: tct(
  78. 'Make sure to import [code1:instrument.js/mjs] at the top of your [code2:main.ts/js] file. Set up the error handler by passing the [code3:BaseExceptionFilter].',
  79. {
  80. code1: <code />,
  81. code2: <code />,
  82. code3: <code />,
  83. docs: (
  84. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
  85. ),
  86. }
  87. ),
  88. code: [
  89. {
  90. label: 'JavaScript',
  91. value: 'javascript',
  92. language: 'javascript',
  93. filename: 'main.(js|ts)',
  94. code: getSdkSetupSnippet(),
  95. },
  96. ],
  97. },
  98. ],
  99. },
  100. getUploadSourceMapsStep({
  101. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/nestjs/sourcemaps/',
  102. ...params,
  103. }),
  104. ],
  105. verify: () => [
  106. {
  107. type: StepType.VERIFY,
  108. description: t(
  109. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  110. ),
  111. configurations: [
  112. {
  113. language: 'javascript',
  114. code: getVerifySnippet(),
  115. },
  116. ],
  117. },
  118. ],
  119. };
  120. const feedbackOnboardingNode: OnboardingConfig = {
  121. introduction: () => getCrashReportApiIntroduction(),
  122. install: () => [
  123. {
  124. type: StepType.INSTALL,
  125. description: getCrashReportInstallDescription(),
  126. configurations: [
  127. {
  128. code: [
  129. {
  130. label: 'JavaScript',
  131. value: 'javascript',
  132. language: 'javascript',
  133. code: `import * as Sentry from "@sentry/node";
  134. const eventId = Sentry.captureMessage("User Feedback");
  135. // OR: const eventId = Sentry.lastEventId();
  136. const userFeedback = {
  137. event_id: eventId,
  138. name: "John Doe",
  139. email: "john@doe.com",
  140. comments: "I really like your App, thanks!",
  141. };
  142. Sentry.captureUserFeedback(userFeedback);
  143. `,
  144. },
  145. ],
  146. },
  147. ],
  148. },
  149. ],
  150. configure: () => [],
  151. verify: () => [],
  152. nextSteps: () => [],
  153. };
  154. const crashReportOnboarding: OnboardingConfig = {
  155. introduction: () => getCrashReportModalIntroduction(),
  156. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  157. configure: () => [
  158. {
  159. type: StepType.CONFIGURE,
  160. description: getCrashReportModalConfigDescription({
  161. link: 'https://docs.sentry.io/platforms/javascript/guides/nestjs/user-feedback/configuration/#crash-report-modal',
  162. }),
  163. },
  164. ],
  165. verify: () => [],
  166. nextSteps: () => [],
  167. };
  168. const docs: Docs = {
  169. onboarding,
  170. feedbackOnboardingCrashApi: feedbackOnboardingNode,
  171. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  172. crashReportOnboarding,
  173. };
  174. export default docs;