|
| 1 | +import { DecodedIntegrationToken } from '@hawk.so/types'; |
| 2 | +import * as Sentry from '@sentry/node'; |
| 3 | +import dotenv from 'dotenv'; |
| 4 | + |
| 5 | +dotenv.config(); |
| 6 | + |
| 7 | +if (!process.env.HAWK_INTEGRATION_TOKEN) { |
| 8 | + throw new Error('Fill HAWK_INTEGRATION_TOKEN in .env file'); |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Decode Hawk integration token |
| 13 | + * |
| 14 | + * @param token - stringified integration token |
| 15 | + */ |
| 16 | +function decodeIntegrationToken(token: string): DecodedIntegrationToken { |
| 17 | + return JSON.parse(Buffer.from(token, 'base64').toString('utf-8')); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Sentry DSN should follow this: |
| 22 | + * const DSN_REGEX = /^(?:(\w+):)\/\/(?:(\w+)(?::(\w+)?)?@)([\w.-]+)(?::(\d+))?\/(.+)/; |
| 23 | + * https://github.com/getsentry/sentry-javascript/blob/d773cb7324480ed3cffc14504f0e41951e344d19/packages/core/src/utils-hoist/dsn.ts#L7 |
| 24 | + * |
| 25 | + * So we can't use our integration token as is. |
| 26 | + * Instead, we will concatinate integrationId and secret and remove hyphens from their uuids. |
| 27 | + */ |
| 28 | +function getHexIntegrationToken(): string { |
| 29 | + const token = process.env.HAWK_INTEGRATION_TOKEN as string; |
| 30 | + |
| 31 | + const { integrationId, secret } = decodeIntegrationToken(token); |
| 32 | + |
| 33 | + const removeHyphens = (str: string): string => str.replace(/-/g, ''); |
| 34 | + |
| 35 | + return `${removeHyphens(integrationId)}${removeHyphens(secret)}`; |
| 36 | +} |
| 37 | + |
| 38 | +const dsn = `https://${getHexIntegrationToken()}@k1.hawk.so/0`; |
| 39 | + |
| 40 | +console.log('dsn', dsn); |
| 41 | + |
| 42 | +/** |
| 43 | + * Initialize Sentry |
| 44 | + */ |
| 45 | +Sentry.init({ |
| 46 | + dsn: `https://${getHexIntegrationToken()}@k1.hawk.so/0`, |
| 47 | + debug: true, |
| 48 | +}); |
| 49 | + |
| 50 | +/** |
| 51 | + * Function that will throw an error |
| 52 | + */ |
| 53 | +function throwDemoError(): void { |
| 54 | + throw new Error('This is a demo error for Sentry!'); |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Main function to run our demo |
| 59 | + */ |
| 60 | +async function main(): Promise<void> { |
| 61 | + try { |
| 62 | + /** |
| 63 | + * Attempt to run function that throws error |
| 64 | + */ |
| 65 | + throwDemoError(); |
| 66 | + } catch (error) { |
| 67 | + /** |
| 68 | + * Capture and send error to Sentry |
| 69 | + */ |
| 70 | + Sentry.captureException(error); |
| 71 | + // console.error('Error caught and sent to Sentry:', error); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Allow time for Sentry to send the error before the process exits |
| 76 | + */ |
| 77 | + // eslint-disable-next-line @typescript-eslint/no-magic-numbers |
| 78 | + await Sentry.close(2000); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Run the demo |
| 83 | + */ |
| 84 | +main() |
| 85 | + .then(() => { |
| 86 | + console.log('✨ Demo completed'); |
| 87 | + }) |
| 88 | + .catch(console.error); |
0 commit comments