From ce60f8c341c96ef354fe2003f21914ecb82eebf5 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 30 May 2022 10:09:10 +0200 Subject: [PATCH] chore(cli): filter usage errors from error reporting --- packages/cdktf-cli/lib/error-reporting.ts | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/cdktf-cli/lib/error-reporting.ts b/packages/cdktf-cli/lib/error-reporting.ts index 441e0eddcc..7c0328b1d5 100644 --- a/packages/cdktf-cli/lib/error-reporting.ts +++ b/packages/cdktf-cli/lib/error-reporting.ts @@ -53,6 +53,14 @@ export async function askForCrashReportingConsent() { return answer.reportCrash; } +function isPromise(p: any): p is Promise { + return ( + typeof p === "object" && + typeof p.then === "function" && + typeof p.catch === "function" + ); +} + export async function initializErrorReporting(askForConsent = false) { let shouldReport = shouldReportCrash(); const ci: string | false = ciDetect(); @@ -83,6 +91,35 @@ export async function initializErrorReporting(askForConsent = false) { autoSessionTracking: true, dsn: process.env.SENTRY_DSN, release: `cdktf-cli-${DISPLAY_VERSION}`, + async beforeSend(event, hint) { + if (!hint) { + return event; + } + + // The promise character is not documented, but it happens + const originalException: + | Promise + | Error + | string + | null + | undefined = hint.originalException; + let error: Error | string | null | undefined; + if (isPromise(originalException)) { + (originalException as unknown as Promise).catch( + (e) => (error = e) + ); + await Promise.allSettled([originalException]); + } else { + error = originalException; + } + + const errorMessage = error?.toString() || ""; + if (errorMessage.includes("Usage Error")) { + // This is a usage error, so we don't want to report it + return null; + } + return event; + }, }); Sentry.configureScope(function (scope) {