connect.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepProps, StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {t} from 'sentry/locale';
  6. import {
  7. getDefaultInitParams,
  8. getDefaultNodeImports,
  9. getInstallSnippet,
  10. getProductInitParams,
  11. getProductIntegrations,
  12. getProductSelectionMap,
  13. joinWithIndentation,
  14. } from 'sentry/utils/gettingStartedDocs/node';
  15. interface StepsParams {
  16. importContent: string;
  17. initContent: string;
  18. installSnippet: string;
  19. sourceMapStep: StepProps;
  20. }
  21. export const steps = ({
  22. installSnippet,
  23. importContent,
  24. initContent,
  25. sourceMapStep,
  26. }: StepsParams): LayoutProps['steps'] => [
  27. {
  28. type: StepType.INSTALL,
  29. description: t('Add the Sentry Node SDK as a dependency:'),
  30. configurations: [
  31. {
  32. language: 'bash',
  33. code: installSnippet,
  34. },
  35. ],
  36. },
  37. {
  38. type: StepType.CONFIGURE,
  39. description: t('Configure Sentry as a middleware:'),
  40. configurations: [
  41. {
  42. language: 'javascript',
  43. code: `
  44. ${importContent}
  45. // Configure Sentry before doing anything else
  46. Sentry.init({
  47. ${initContent}
  48. });
  49. function mainHandler(req, res) {
  50. throw new Error("My first Sentry error!");
  51. }
  52. function onError(err, req, res, next) {
  53. // The error id is attached to \`res.sentry\` to be returned
  54. // and optionally displayed to the user for support.
  55. res.statusCode = 500;
  56. res.end(res.sentry + "\\n");
  57. }
  58. connect(
  59. // The request handler be the first item
  60. Sentry.Handlers.requestHandler(),
  61. connect.bodyParser(),
  62. connect.cookieParser(),
  63. mainHandler,
  64. // The error handler must be before any other error middleware
  65. Sentry.Handlers.errorHandler(),
  66. // Optional fallthrough error handler
  67. onError
  68. ).listen(3000);
  69. `,
  70. },
  71. ],
  72. },
  73. sourceMapStep,
  74. ];
  75. export function GettingStartedWithConnect({
  76. dsn,
  77. newOrg,
  78. platformKey,
  79. activeProductSelection = [],
  80. organization,
  81. projectId,
  82. }: ModuleProps) {
  83. const productSelection = getProductSelectionMap(activeProductSelection);
  84. const installSnippet = getInstallSnippet({productSelection});
  85. const imports = getDefaultNodeImports({productSelection});
  86. imports.push('import connect from "connect";');
  87. const integrations = getProductIntegrations({productSelection});
  88. const integrationParam =
  89. integrations.length > 0
  90. ? `integrations: [\n${joinWithIndentation(integrations)}\n],`
  91. : null;
  92. const initContent = joinWithIndentation([
  93. ...getDefaultInitParams({dsn}),
  94. ...(integrationParam ? [integrationParam] : []),
  95. ...getProductInitParams({productSelection}),
  96. ]);
  97. return (
  98. <Layout
  99. steps={steps({
  100. installSnippet,
  101. importContent: imports.join('\n'),
  102. initContent,
  103. sourceMapStep: getUploadSourceMapsStep({
  104. guideLink: 'https://docs.sentry.io/platforms/node/guides/connect/sourcemaps/',
  105. organization,
  106. platformKey,
  107. projectId,
  108. newOrg,
  109. }),
  110. })}
  111. newOrg={newOrg}
  112. platformKey={platformKey}
  113. />
  114. );
  115. }
  116. export default GettingStartedWithConnect;