nestjs.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 {t, tct} from 'sentry/locale';
  17. import {
  18. getImportInstrumentSnippet,
  19. getInstallConfig,
  20. getSdkInitSnippet,
  21. } from 'sentry/utils/gettingStartedDocs/node';
  22. type Params = DocsParams;
  23. const getSdkSetupSnippet = () => `
  24. ${getImportInstrumentSnippet('esm', 'ts')}
  25. // All other imports below
  26. import { NestFactory } from '@nestjs/core';
  27. import { AppModule } from './app.module';
  28. async function bootstrap() {
  29. const app = await NestFactory.create(AppModule);
  30. await app.listen(3000);
  31. }
  32. bootstrap();
  33. `;
  34. const getAppModuleSnippet = () => `
  35. import { Module } from '@nestjs/common';
  36. import { SentryModule } from '@sentry/nestjs/setup';
  37. import { AppController } from './app.controller';
  38. import { AppService } from './app.service';
  39. @Module({
  40. imports: [
  41. SentryModule.forRoot(),
  42. // ...other modules
  43. ],
  44. controllers: [AppController],
  45. providers: [AppService],
  46. })
  47. export class AppModule {}
  48. `;
  49. const getVerifySnippet = () => `
  50. @Get("/debug-sentry")
  51. getError() {
  52. throw new Error("My first Sentry error!");
  53. }
  54. `;
  55. const getDecoratedGlobalFilter =
  56. () => `import { Catch, ExceptionFilter } from '@nestjs/common';
  57. import { SentryExceptionCaptured } from '@sentry/nestjs';
  58. @Catch()
  59. export class YourCatchAllExceptionFilter implements ExceptionFilter {
  60. @SentryExceptionCaptured()
  61. catch(exception, host): void {
  62. // your implementation here
  63. }
  64. }
  65. `;
  66. const getAppModuleSnippetWithSentryGlobalFilter =
  67. () => `import { Module } from '@nestjs/common';
  68. import { APP_FILTER } from '@nestjs/core';
  69. import { SentryGlobalFilter } from '@sentry/nestjs/setup';
  70. @Module({
  71. providers: [
  72. {
  73. provide: APP_FILTER,
  74. useClass: SentryGlobalFilter,
  75. },
  76. // ..other providers
  77. ],
  78. })
  79. export class AppModule {}
  80. `;
  81. const onboarding: OnboardingConfig = {
  82. introduction: () =>
  83. tct('In this quick guide you’ll use [strong:npm] or [strong:yarn] to set up:', {
  84. strong: <strong />,
  85. }),
  86. install: params => [
  87. {
  88. type: StepType.INSTALL,
  89. description: t('Add the Sentry NestJS SDK as a dependency:'),
  90. configurations: getInstallConfig(params, {
  91. basePackage: '@sentry/nestjs',
  92. }),
  93. },
  94. ],
  95. configure: params => [
  96. {
  97. type: StepType.CONFIGURE,
  98. description: t(
  99. "Initialize Sentry as early as possible in your application's lifecycle."
  100. ),
  101. configurations: [
  102. {
  103. description: tct(
  104. 'To initialize the SDK before everything else, create a file called [code:instrument.ts] in your [code:src/] folder.',
  105. {code: <code />}
  106. ),
  107. code: [
  108. {
  109. label: 'TypeScript',
  110. value: 'javascript',
  111. language: 'javascript',
  112. filename: 'instrument.ts',
  113. code: getSdkInitSnippet(params, 'nestjs', 'esm'),
  114. },
  115. ],
  116. },
  117. {
  118. description: tct(
  119. 'Make sure to import [code:instrument.ts] at the top of your [code:main.ts] file:',
  120. {
  121. code: <code />,
  122. docs: (
  123. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
  124. ),
  125. }
  126. ),
  127. code: [
  128. {
  129. label: 'TypeScript',
  130. value: 'javascript',
  131. language: 'javascript',
  132. filename: 'main.ts',
  133. code: getSdkSetupSnippet(),
  134. },
  135. ],
  136. },
  137. {
  138. description: tct(
  139. 'Add the [code:SentryModule] as a root module to your main module:',
  140. {
  141. code: <code />,
  142. docs: (
  143. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/nestjs/install/" />
  144. ),
  145. }
  146. ),
  147. code: [
  148. {
  149. label: 'TypeScript',
  150. value: 'javascript',
  151. language: 'javascript',
  152. filename: 'app.module.ts',
  153. code: getAppModuleSnippet(),
  154. },
  155. ],
  156. },
  157. {
  158. description: tct(
  159. 'If you are using a global catch-all exception filter add a [code:@SentryExceptionCaptured()] decorator to the [code:catch()] method of this global error filter. This will report all unhandled errors to Sentry',
  160. {
  161. code: <code />,
  162. }
  163. ),
  164. code: [
  165. {
  166. label: 'TypeScript',
  167. value: 'javascript',
  168. language: 'javascript',
  169. filename: 'global.filter.ts',
  170. code: getDecoratedGlobalFilter(),
  171. },
  172. ],
  173. },
  174. {
  175. description: tct(
  176. 'Alternatively, add the [code:SentryGlobalFilter] (or [code:SentryGlobalGraphQLFilter] if you are using GraphQL) before any other exception filters to the providers of your main module.',
  177. {
  178. code: <code />,
  179. }
  180. ),
  181. code: [
  182. {
  183. label: 'TypeScript',
  184. value: 'javascript',
  185. language: 'javascript',
  186. filename: 'app.module.ts',
  187. code: getAppModuleSnippetWithSentryGlobalFilter(),
  188. },
  189. ],
  190. },
  191. ],
  192. },
  193. getUploadSourceMapsStep({
  194. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/nestjs/sourcemaps/',
  195. ...params,
  196. }),
  197. ],
  198. verify: () => [
  199. {
  200. type: StepType.VERIFY,
  201. description: t(
  202. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  203. ),
  204. configurations: [
  205. {
  206. language: 'javascript',
  207. code: getVerifySnippet(),
  208. },
  209. ],
  210. },
  211. ],
  212. };
  213. const feedbackOnboardingNode: OnboardingConfig = {
  214. introduction: () => getCrashReportApiIntroduction(),
  215. install: () => [
  216. {
  217. type: StepType.INSTALL,
  218. description: getCrashReportInstallDescription(),
  219. configurations: [
  220. {
  221. code: [
  222. {
  223. label: 'TypeScript',
  224. value: 'javascript',
  225. language: 'javascript',
  226. code: `import * as Sentry from "@sentry/node";
  227. const eventId = Sentry.captureMessage("User Feedback");
  228. // OR: const eventId = Sentry.lastEventId();
  229. const userFeedback = {
  230. event_id: eventId,
  231. name: "John Doe",
  232. email: "john@doe.com",
  233. comments: "I really like your App, thanks!",
  234. };
  235. Sentry.captureUserFeedback(userFeedback);
  236. `,
  237. },
  238. ],
  239. },
  240. ],
  241. },
  242. ],
  243. configure: () => [],
  244. verify: () => [],
  245. nextSteps: () => [],
  246. };
  247. const crashReportOnboarding: OnboardingConfig = {
  248. introduction: () => getCrashReportModalIntroduction(),
  249. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  250. configure: () => [
  251. {
  252. type: StepType.CONFIGURE,
  253. description: getCrashReportModalConfigDescription({
  254. link: 'https://docs.sentry.io/platforms/javascript/guides/nestjs/user-feedback/configuration/#crash-report-modal',
  255. }),
  256. },
  257. ],
  258. verify: () => [],
  259. nextSteps: () => [],
  260. };
  261. const profilingOnboarding: OnboardingConfig = {
  262. ...onboarding,
  263. introduction: () => null,
  264. };
  265. const docs: Docs = {
  266. onboarding,
  267. feedbackOnboardingCrashApi: feedbackOnboardingNode,
  268. crashReportOnboarding,
  269. profilingOnboarding,
  270. };
  271. export default docs;