Skip to content

Commit 8bded28

Browse files
authored
fix: (CN-943) wrap GoFileName error (#772)
* fix: wrap go parser error
1 parent e1909ff commit 8bded28

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

.snyk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ ignore:
55
SNYK-JS-TAR-15307072:
66
- 'snyk-nodejs-lockfile-parser > @yarnpkg/core > tar':
77
reason: 'Indirect dependency from snyk-nodejs-lockfile-parser, waiting for upstream fix'
8-
expires: 2026-03-26T00:00:00.000Z
8+
expires: 2026-04-03T00:00:00.000Z
99
SNYK-JS-TAR-15416075:
1010
- 'snyk-nodejs-lockfile-parser > @yarnpkg/core > tar':
1111
reason: 'Indirect dependency from snyk-nodejs-lockfile-parser, waiting for upstream fix'
12-
expires: 2026-03-26T00:00:00.000Z
12+
expires: 2026-04-03T00:00:00.000Z
1313
SNYK-JS-TAR-15456201:
1414
- 'snyk-nodejs-lockfile-parser > @yarnpkg/core > tar':
1515
reason: 'Indirect dependency from snyk-nodejs-lockfile-parser, waiting for upstream fix'
16-
expires: 2026-03-26T00:00:00.000Z
16+
expires: 2026-04-03T00:00:00.000Z
1717
patch: {}

lib/go-parser/go-binary.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ export class GoBinary {
169169
// result in the package name "github.com/my/pkg/a".
170170
const parts = pkgFile.split(modFullName);
171171
if (parts.length !== 2 || parts[0] !== "") {
172-
throw {
173-
fileName: pkgFile,
174-
moduleName: modFullName,
175-
} as GoFileNameError;
172+
throw new GoFileNameError(pkgFile, modFullName);
176173
}
177174

178175
// for files in the "root" of a module
@@ -194,9 +191,17 @@ export class GoBinary {
194191
}
195192
}
196193

197-
interface GoFileNameError extends Error {
198-
fileName: string;
199-
moduleName: string;
194+
export class GoFileNameError extends Error {
195+
public readonly fileName: string;
196+
public readonly moduleName: string;
197+
198+
constructor(fileName: string, moduleName: string) {
199+
super();
200+
this.name = "GoFileNameError";
201+
this.message = `Failed to match Go file "${fileName}" to module "${moduleName}"`;
202+
this.fileName = fileName;
203+
this.moduleName = moduleName;
204+
}
200205
}
201206

202207
/**

test/unit/go-binaries.spec.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
determinePaths,
99
extractModuleInformation,
1010
GoBinary,
11+
GoFileNameError,
1112
parseGoVersion,
1213
} from "../../lib/go-parser/go-binary";
1314
import { GoModule } from "../../lib/go-parser/go-module";
@@ -659,3 +660,77 @@ describe("test path determination", () => {
659660
expect(vendorPath).toBe("");
660661
});
661662
});
663+
664+
describe("GoFileNameError", () => {
665+
// Construct a GoBinary from any real fixture so we have a valid instance to
666+
// call matchFilesToModules on. We then overwrite modules directly.
667+
const goBin = new GoBinary(
668+
elf.parse(
669+
readFileSync(
670+
path.join(__dirname, "../fixtures/go-binaries/go1.13.15_normal"),
671+
),
672+
),
673+
);
674+
675+
const moduleName = "github.com/foo/bar";
676+
const moduleVersion = "v1.0.0";
677+
678+
// Crafting a file path where the module's fullName appears twice causes
679+
// pkgFile.split(modFullName) to produce 3 parts, triggering the throw.
680+
const duplicatedPath = `${moduleName}@${moduleVersion}/${moduleName}@${moduleVersion}/file.go`;
681+
682+
beforeEach(() => {
683+
goBin.modules = [new GoModule(moduleName, moduleVersion)];
684+
});
685+
686+
it("throws GoFileNameError when a file path cannot be matched to a module", () => {
687+
expect(() => goBin.matchFilesToModules([duplicatedPath])).toThrow(
688+
GoFileNameError,
689+
);
690+
});
691+
692+
it("thrown error is an instance of Error", () => {
693+
expect(() => goBin.matchFilesToModules([duplicatedPath])).toThrow(Error);
694+
});
695+
696+
it("thrown error has a non-empty message containing the file and module names", () => {
697+
try {
698+
goBin.matchFilesToModules([duplicatedPath]);
699+
} catch (err) {
700+
expect(err).toBeInstanceOf(GoFileNameError);
701+
expect((err as GoFileNameError).message).toBeTruthy();
702+
expect((err as GoFileNameError).message).toContain(duplicatedPath);
703+
expect((err as GoFileNameError).message).toContain(
704+
`${moduleName}@${moduleVersion}`,
705+
);
706+
}
707+
});
708+
709+
it("thrown error has a stack trace", () => {
710+
try {
711+
goBin.matchFilesToModules([duplicatedPath]);
712+
} catch (err) {
713+
expect((err as GoFileNameError).stack).toBeDefined();
714+
}
715+
});
716+
717+
it("thrown error carries fileName and moduleName properties", () => {
718+
try {
719+
goBin.matchFilesToModules([duplicatedPath]);
720+
} catch (err) {
721+
expect(err).toBeInstanceOf(GoFileNameError);
722+
expect((err as GoFileNameError).fileName).toBe(duplicatedPath);
723+
expect((err as GoFileNameError).moduleName).toBe(
724+
`${moduleName}@${moduleVersion}`,
725+
);
726+
}
727+
});
728+
729+
it("thrown error has name set to GoFileNameError", () => {
730+
try {
731+
goBin.matchFilesToModules([duplicatedPath]);
732+
} catch (err) {
733+
expect((err as GoFileNameError).name).toBe("GoFileNameError");
734+
}
735+
});
736+
});

0 commit comments

Comments
 (0)