connect.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  2. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  3. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  4. import {getUploadSourceMapsStep} from 'sentry/components/onboarding/gettingStartedDoc/utils';
  5. import {PlatformKey} from 'sentry/data/platformCategories';
  6. import {t, tct} from 'sentry/locale';
  7. import type {Organization} from 'sentry/types';
  8. type StepProps = {
  9. newOrg: boolean;
  10. organization: Organization;
  11. platformKey: PlatformKey;
  12. projectId: string;
  13. sentryInitContent: string;
  14. };
  15. export const steps = ({
  16. sentryInitContent,
  17. ...props
  18. }: Partial<StepProps> = {}): LayoutProps['steps'] => [
  19. {
  20. type: StepType.INSTALL,
  21. description: (
  22. <p>{tct('Add [code:@sentry/node] as a dependency:', {code: <code />})}</p>
  23. ),
  24. configurations: [
  25. {
  26. language: 'bash',
  27. code: `
  28. # Using yarn
  29. yarn add @sentry/node
  30. # Using npm
  31. npm install --save @sentry/node
  32. `,
  33. },
  34. ],
  35. },
  36. {
  37. type: StepType.CONFIGURE,
  38. description: t('Configure Sentry as a middleware:'),
  39. configurations: [
  40. {
  41. language: 'javascript',
  42. code: `
  43. import * as Sentry from "@sentry/node";
  44. import connect from "connect";
  45. // or using CommonJS
  46. // const connect = require('connect');
  47. // const Sentry = require('@sentry/node');
  48. // Configure Sentry before doing anything else
  49. Sentry.init({
  50. ${sentryInitContent},
  51. });
  52. function mainHandler(req, res) {
  53. throw new Error("My first Sentry error!");
  54. }
  55. function onError(err, req, res, next) {
  56. // The error id is attached to \`res.sentry\` to be returned
  57. // and optionally displayed to the user for support.
  58. res.statusCode = 500;
  59. res.end(res.sentry + "\\n");
  60. }
  61. connect(
  62. // The request handler be the first item
  63. Sentry.Handlers.requestHandler(),
  64. connect.bodyParser(),
  65. connect.cookieParser(),
  66. mainHandler,
  67. // The error handler must be before any other error middleware
  68. Sentry.Handlers.errorHandler(),
  69. // Optional fallthrough error handler
  70. onError
  71. ).listen(3000);
  72. `,
  73. },
  74. ],
  75. },
  76. getUploadSourceMapsStep({
  77. guideLink: 'https://docs.sentry.io/platforms/node/guides/connect/sourcemaps/',
  78. ...props,
  79. }),
  80. ];
  81. export function GettingStartedWithConnect({
  82. dsn,
  83. organization,
  84. newOrg,
  85. platformKey,
  86. projectId,
  87. }: ModuleProps) {
  88. const sentryInitContent: string[] = [`dsn: "${dsn}"`];
  89. return (
  90. <Layout
  91. steps={steps({
  92. sentryInitContent: sentryInitContent.join('\n'),
  93. organization,
  94. newOrg,
  95. platformKey,
  96. projectId,
  97. })}
  98. newOrg={newOrg}
  99. platformKey={platformKey}
  100. />
  101. );
  102. }
  103. export default GettingStartedWithConnect;