Skip to content

Commit

Permalink
Merge pull request #1828 from hashicorp/ignore-usage-errors
Browse files Browse the repository at this point in the history
chore(cli): filter usage errors from error reporting
  • Loading branch information
DanielMSchmidt authored Jun 2, 2022
2 parents 6c2297d + b1928e5 commit 1788df6
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/cdktf-cli/lib/error-reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export async function askForCrashReportingConsent() {
return answer.reportCrash;
}

function isPromise(p: any): p is Promise<any> {
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();
Expand Down Expand Up @@ -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>
| Error
| string
| null
| undefined = hint.originalException;
let error: Error | string | null | undefined;
if (isPromise(originalException)) {
(originalException as unknown as Promise<Error>).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) {
Expand Down

0 comments on commit 1788df6

Please sign in to comment.