hapi.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. getCrashReportApiIntroduction,
  11. getCrashReportInstallDescription,
  12. getCrashReportJavaScriptInstallStep,
  13. getCrashReportModalConfigDescription,
  14. getCrashReportModalIntroduction,
  15. } from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  16. import {getJSServerMetricsOnboarding} from 'sentry/components/onboarding/gettingStartedDoc/utils/metricsOnboarding';
  17. import {t, tct} from 'sentry/locale';
  18. import {
  19. getImportInstrumentSnippet,
  20. getInstallConfig,
  21. getSdkInitSnippet,
  22. getSentryImportSnippet,
  23. } from 'sentry/utils/gettingStartedDocs/node';
  24. type Params = DocsParams;
  25. const getSdkSetupSnippet = () => `
  26. ${getImportInstrumentSnippet()}
  27. // All other imports below
  28. ${getSentryImportSnippet('node')}
  29. const Hapi = require('@hapi/hapi');
  30. const init = async () => {
  31. const server = Hapi.server({
  32. port: 3030,
  33. host: 'localhost',
  34. });
  35. // All your routes live here
  36. await Sentry.setupHapiErrorHandler(server);
  37. await server.start();
  38. };
  39. init();
  40. `;
  41. const getVerifySnippet = () => `
  42. app.use(async function () {
  43. throw new Error("My first Sentry error!");
  44. });
  45. `;
  46. const onboarding: OnboardingConfig = {
  47. install: params => [
  48. {
  49. type: StepType.INSTALL,
  50. description: t('Add the Sentry Node SDK as a dependency:'),
  51. configurations: getInstallConfig(params),
  52. },
  53. ],
  54. configure: params => [
  55. {
  56. type: StepType.CONFIGURE,
  57. description: t(
  58. "Initialize Sentry as early as possible in your application's lifecycle. Otherwise, auto-instrumentation will not work."
  59. ),
  60. configurations: [
  61. {
  62. description: tct(
  63. 'To initialize the SDK before everything else, create an external file called [code:instrument.js/mjs].',
  64. {code: <code />}
  65. ),
  66. code: [
  67. {
  68. label: 'JavaScript',
  69. value: 'javascript',
  70. language: 'javascript',
  71. filename: 'instrument.(js|mjs)',
  72. code: getSdkInitSnippet(params, 'node'),
  73. },
  74. ],
  75. },
  76. {
  77. description: tct(
  78. "Make sure to import [code1:instrument.js/mjs] at the top of your file. Set up the error handler. 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].",
  79. {
  80. code1: <code />,
  81. code2: <code />,
  82. docs: (
  83. <ExternalLink href="https://docs.sentry.io/platforms/javascript/guides/hapi/install/" />
  84. ),
  85. }
  86. ),
  87. code: [
  88. {
  89. label: 'JavaScript',
  90. value: 'javascript',
  91. language: 'javascript',
  92. filename: 'index.(js|mjs)',
  93. code: getSdkSetupSnippet(),
  94. },
  95. ],
  96. },
  97. ],
  98. },
  99. getUploadSourceMapsStep({
  100. guideLink: 'https://docs.sentry.io/platforms/javascript/guides/hapi/sourcemaps/',
  101. ...params,
  102. }),
  103. ],
  104. verify: () => [
  105. {
  106. type: StepType.VERIFY,
  107. description: t(
  108. "This snippet contains an intentional error and can be used as a test to make sure that everything's working as expected."
  109. ),
  110. configurations: [
  111. {
  112. language: 'javascript',
  113. code: getVerifySnippet(),
  114. },
  115. ],
  116. },
  117. ],
  118. };
  119. const feedbackOnboardingNode: OnboardingConfig = {
  120. introduction: () => getCrashReportApiIntroduction(),
  121. install: () => [
  122. {
  123. type: StepType.INSTALL,
  124. description: getCrashReportInstallDescription(),
  125. configurations: [
  126. {
  127. code: [
  128. {
  129. label: 'JavaScript',
  130. value: 'javascript',
  131. language: 'javascript',
  132. code: `import * as Sentry from "@sentry/node";
  133. const eventId = Sentry.captureMessage("User Feedback");
  134. // OR: const eventId = Sentry.lastEventId();
  135. const userFeedback = {
  136. event_id: eventId,
  137. name: "John Doe",
  138. email: "john@doe.com",
  139. comments: "I really like your App, thanks!",
  140. };
  141. Sentry.captureUserFeedback(userFeedback);
  142. `,
  143. },
  144. ],
  145. },
  146. ],
  147. },
  148. ],
  149. configure: () => [],
  150. verify: () => [],
  151. nextSteps: () => [],
  152. };
  153. const crashReportOnboarding: OnboardingConfig = {
  154. introduction: () => getCrashReportModalIntroduction(),
  155. install: (params: Params) => getCrashReportJavaScriptInstallStep(params),
  156. configure: () => [
  157. {
  158. type: StepType.CONFIGURE,
  159. description: getCrashReportModalConfigDescription({
  160. link: 'https://docs.sentry.io/platforms/javascript/guides/hapi/user-feedback/configuration/#crash-report-modal',
  161. }),
  162. },
  163. ],
  164. verify: () => [],
  165. nextSteps: () => [],
  166. };
  167. const docs: Docs = {
  168. onboarding,
  169. feedbackOnboardingCrashApi: feedbackOnboardingNode,
  170. customMetricsOnboarding: getJSServerMetricsOnboarding(),
  171. crashReportOnboarding,
  172. };
  173. export default docs;