connect.tsx 2.6 KB

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