Skip to content

Commit d67e83f

Browse files
do not send to Sentry errors that are raised from external packages
1 parent b6b8277 commit d67e83f

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

v-next/hardhat/src/internal/cli/telemetry/sentry/anonymizer.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,14 @@ export class Anonymizer {
198198
}
199199

200200
#isHardhatFile(filename: string): boolean {
201+
const totNodeModules = filename.split("node_modules").length - 1;
202+
201203
const nomicFoundationPath = path.join("node_modules", "@nomicfoundation");
202-
return filename.startsWith(nomicFoundationPath);
204+
if (filename.startsWith(nomicFoundationPath) && totNodeModules === 1) {
205+
return true;
206+
}
207+
208+
return false;
203209
}
204210

205211
async #anonymizeExceptions(exceptions: Exception[]): Promise<Exception[]> {

v-next/hardhat/test/internal/cli/telemetry/sentry/anonymizer.ts

+58
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,62 @@ describe("Anonymizer", () => {
368368
);
369369
});
370370
});
371+
372+
describe("raisedByHardhat", () => {
373+
function createTestEvent(filePath: string) {
374+
return {
375+
exception: {
376+
values: [
377+
{
378+
type: "Error",
379+
value: "test-error",
380+
stacktrace: {
381+
frames: [
382+
{
383+
filename: "<user-file>",
384+
},
385+
{
386+
filename: "<user-file>",
387+
},
388+
{
389+
filename: filePath,
390+
},
391+
],
392+
},
393+
},
394+
],
395+
},
396+
};
397+
}
398+
399+
it("should return true because the error was raised by hardhat", () => {
400+
const anonymizer = new Anonymizer();
401+
const res = anonymizer.raisedByHardhat(
402+
createTestEvent(
403+
"node_modules/@nomicfoundation/random-path/some-file.js",
404+
),
405+
);
406+
assert.equal(res, true);
407+
});
408+
409+
it("should return false because the error was not raised by hardhat", () => {
410+
const anonymizer = new Anonymizer();
411+
const res = anonymizer.raisedByHardhat(
412+
createTestEvent(
413+
"node_modules/@random-npm-package/random-path/some-file.js",
414+
),
415+
);
416+
assert.equal(res, false);
417+
});
418+
419+
it("should return false because the error was raised inside of hardhat BUT from an external package", () => {
420+
const anonymizer = new Anonymizer();
421+
const res = anonymizer.raisedByHardhat(
422+
createTestEvent(
423+
"node_modules/@nomicfoundation/node_modules/@random-npm-package/errors.ts",
424+
),
425+
);
426+
assert.equal(res, false);
427+
});
428+
});
371429
});

0 commit comments

Comments
 (0)