express.tsx 4.4 KB

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