Skip to content

Commit

Permalink
[INTERNAL] Application: Re-throw ENOENT error as-is
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Jul 13, 2023
1 parent 7bc4e10 commit f57ddbd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
11 changes: 6 additions & 5 deletions lib/specifications/types/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ class Application extends ComponentProject {
try {
return await this._getNamespaceFromManifestJson();
} catch (manifestJsonError) {
if (manifestJsonError.cause?.code !== "ENOENT") {
if (manifestJsonError.code !== "ENOENT") {
throw manifestJsonError;
}
// No manifest.json present
// => attempt fallback to manifest.appdescr_variant (typical for App Variants)
try {
return await this._getNamespaceFromManifestAppDescVariant();
} catch (appDescVarError) {
if (appDescVarError.cause?.code === "ENOENT") {
if (appDescVarError.code === "ENOENT") {
// Fallback not possible: No manifest.appdescr_variant present
// => Throw error indicating missing manifest.json
// (do not mention manifest.appdescr_variant since it is only
Expand Down Expand Up @@ -221,11 +221,12 @@ class Application extends ComponentProject {
}
return JSON.parse(await resource.getString());
}).catch((err) => {
if (err.code === "ENOENT") {
throw err;
}
throw new Error(
`Failed to read ${filePath} for project ` +
`${this.getName()}: ${err.message}`, {
cause: err
});
`${this.getName()}: ${err.message}`);
});
}
}
Expand Down
13 changes: 5 additions & 8 deletions test/lib/specifications/types/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import Application from "../../../../lib/specifications/types/Application.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const applicationAPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.a");
const applicationHPath = path.join(__dirname, "..", "..", "..", "fixtures", "application.h");
const ENOENT_ERROR = new Error();
ENOENT_ERROR.code = "ENOENT";

test.beforeEach((t) => {
t.context.sinon = sinonGlobal.createSandbox();
Expand Down Expand Up @@ -491,7 +489,7 @@ test("_getNamespace: Correct fallback to manifest.appdescr_variant if manifest.j
const {projectInput, sinon} = t.context;
const project = await Specification.create(projectInput);
const _getManifestStub = sinon.stub(project, "_getManifest")
.onFirstCall().rejects(new Error("", {cause: ENOENT_ERROR}))
.onFirstCall().rejects({code: "ENOENT"})
.onSecondCall().resolves({id: "my.id"});

const namespace = await project._getNamespace();
Expand All @@ -506,7 +504,7 @@ test("_getNamespace: Correct error message if fallback to manifest.appdescr_vari
const {projectInput, sinon} = t.context;
const project = await Specification.create(projectInput);
const _getManifestStub = sinon.stub(project, "_getManifest")
.onFirstCall().rejects(new Error("", {cause: ENOENT_ERROR}))
.onFirstCall().rejects({code: "ENOENT"})
.onSecondCall().rejects(new Error("EPON: Pony Error"));

const error = await t.throwsAsync(project._getNamespace());
Expand All @@ -522,8 +520,8 @@ test("_getNamespace: Correct error message if fallback to manifest.appdescr_vari
const {projectInput, sinon} = t.context;
const project = await Specification.create(projectInput);
const _getManifestStub = sinon.stub(project, "_getManifest")
.onFirstCall().rejects(new Error("No such stable or directory: manifest.json", {cause: ENOENT_ERROR}))
.onSecondCall().rejects(new Error("", {cause: ENOENT_ERROR})); // both files are missing
.onFirstCall().rejects({message: "No such stable or directory: manifest.json", code: "ENOENT"})
.onSecondCall().rejects({code: "ENOENT"}); // both files are missing

const error = await t.throwsAsync(project._getNamespace());
t.deepEqual(error.message,
Expand Down Expand Up @@ -586,10 +584,9 @@ test.serial("_getManifest: File does not exist", async (t) => {

const error = await t.throwsAsync(project._getManifest("/does-not-exist.json"));
t.is(error.message,
"Failed to read /does-not-exist.json for project application.a: " +
"Could not find resource /does-not-exist.json in project application.a",
"Rejected with correct error message");
t.is(error.cause.code, "ENOENT");
t.is(error.code, "ENOENT");
});

test.serial("_getManifest: result is cached", async (t) => {
Expand Down

0 comments on commit f57ddbd

Please sign in to comment.