express.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. getCrashReportJavaScriptInstallStep,
  11. getCrashReportModalConfigDescription,
  12. getCrashReportModalIntroduction,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  14. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  15. import replayOnboardingJsLoader from 'sentry/gettingStartedDocs/javascript/jsLoader/jsLoader';
  16. import {t, tct} from 'sentry/locale';
  17. import {
  18. getImportInstrumentSnippet,
  19. getInstallConfig,
  20. getSdkInitSnippet,
  21. getSentryImportSnippet,
  22. } from 'sentry/utils/gettingStartedDocs/node';
  23. type Params = DocsParams;
  24. const getSdkSetupSnippet = () => `
  25. ${getImportInstrumentSnippet()}
  26. // All other imports below
  27. ${getSentryImportSnippet('node')}
  28. const express = require("express");
  29. const app = express();
  30. // All your controllers should live here
  31. app.get("/", function rootHandler(req, res) {
  32. res.end("Hello world!");
  33. });
  34. // The error handler must be registered before any other error middleware and after all controllers
  35. Sentry.setupExpressErrorHandler(app);
  36. // Optional fallthrough error handler
  37. app.use(function onError(err, req, res, next) {
  38. // The error id is attached to \`res.sentry\` to be returned
  39. // and optionally displayed to the user for support.
  40. res.statusCode = 500;
  41. res.end(res.sentry + "\\n");
  42. });
  43. app.listen(3000);
  44. `;
  45. const onboarding: OnboardingConfig = {
  46. install: (params: Params) => [
  47. {
  48. type: StepType.INSTALL,
  49. description: t('Add the Sentry Node SDK as a dependency:'),
  50. configurations: getInstallConfig(params),
  51. },
  52. ],
  53. configure: (params: Params) => [
  54. {
  55. type: StepType.CONFIGURE,
  56. description: t(
  57. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  58. ),
  59. configurations: [
  60. {
  61. description: tct(
  62. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  63. {code: <code />}
  64. ),
  65. code: [
  66. {
  67. label: 'JavaScript',
  68. value: 'javascript',
  69. language: 'javascript',
  70. filename: 'instrument.(js|mjs)',
  71. code: getSdkInitSnippet(params, 'node'),
  72. },
  73. ],
  74. },
  75. {
  76. description: tct(
  77. "Make sure to import [code1:instrument.js/mjs] at the top of your file. Set up the error handler after all controllers and before any other error middleware. This setup is typically done in your application's entry point file, which is usually [code2:index.(js|ts)]. If you're running your application in ESM mode, or looking for alternative ways to set up Sentry, read about [docs:installation methods in our docs].",
  78. {
  79. code1: <code />,
  80. code2: <code />,
  81. docs: (
  82. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/express/install/" />
  83. ),
  84. }
  85. ),
  86. code: [
  87. {
  88. label: 'JavaScript',
  89. value: 'javascript',
  90. language: 'javascript',
  91. filename: 'index.(js|mjs)',
  92. code: getSdkSetupSnippet(),
  93. },
  94. ],
  95. },
  96. ],
  97. },
  98. getUploadSourceMapsStep({
  99. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/express/sourcemaps/',
  100. ...params,
  101. }),
  102. ],
  103. verify: () => [
  104. {
  105. type: StepType.VERIFY,
  106. description: t(
  107. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  108. ),
  109. configurations: [
  110. {
  111. language: 'javascript',
  112. code: `
  113. app.get("/debug-sentry", function mainHandler(req, res) {
  114. throw new Error("My first Sentry error!");
  115. });
  116. `,
  117. },
  118. ],
  119. },
  120. ],
  121. };
  122. const crashReportOnboarding: OnboardingConfig = {
  123. introduction: () => getCrashReportModalIntroduction(),
  124. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  125. configure: () => [
  126. {
  127. type: StepType.CONFIGURE,
  128. description: getCrashReportModalConfigDescription({
  129. link: 'https://docs.sentry.io/platforms/javascript/guides/express/user-feedback/configuration/#crash-report-modal',
  130. }),
  131. },
  132. ],
  133. verify: () => [],
  134. nextSteps: () => [],
  135. };
  136. const docs: Docs = {
  137. onboarding,
  138. replayOnboardingJsLoader,
  139. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  140. crashReportOnboarding,
  141. };
  142. export default docs;