Skip to content

Commit 5b4b1bc

Browse files
committed
fix: set the cause of file parsing errors to the error value
1 parent 20f425b commit 5b4b1bc

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export default class Content {
141141
config.core,
142142
"Invalid input! Failed to parse contents of the provided payload file",
143143
{
144-
cause: error,
144+
cause: { values: [error] },
145145
},
146146
);
147147
}

test/content.spec.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,63 @@ describe("content", () => {
340340
err.message,
341341
"Invalid input! Failed to parse contents of the provided payload file",
342342
);
343+
assert.isDefined(err.cause?.values);
344+
assert.equal(err.cause.values.length, 1);
343345
assert.include(
344-
err.cause.message,
346+
err.cause.values[0].message,
345347
"Invalid input! Failed to parse file extension unknown.md",
346348
);
347349
} else {
348350
assert.fail("Failed to throw a SlackError", err);
349351
}
350352
}
351353
});
354+
355+
it("fails if invalid JSON exists in the input payload", async () => {
356+
mocks.core.getInput.withArgs("payload-file-path").returns("example.json");
357+
mocks.fs.readFileSync
358+
.withArgs(path.resolve("example.json"), "utf-8")
359+
.returns(`{
360+
"message": "a truncated file without an end`);
361+
try {
362+
await send(mocks.core);
363+
assert.fail("Failed to throw for invalid JSON");
364+
} catch (err) {
365+
if (err instanceof SlackError) {
366+
assert.include(
367+
err.message,
368+
"Invalid input! Failed to parse contents of the provided payload file",
369+
);
370+
assert.isDefined(err.cause?.values);
371+
assert.equal(err.cause.values.length, 1);
372+
assert.isTrue(err.cause.values[0] instanceof SyntaxError);
373+
} else {
374+
assert.fail("Failed to throw a SlackError", err);
375+
}
376+
}
377+
});
378+
379+
it("fails if invalid YAML exists in the input payload", async () => {
380+
mocks.core.getInput.withArgs("payload-file-path").returns("example.yaml");
381+
mocks.fs.readFileSync
382+
.withArgs(path.resolve("example.yaml"), "utf-8")
383+
.returns(`- "message": "assigned": "values"`);
384+
try {
385+
await send(mocks.core);
386+
assert.fail("Failed to throw for invalid YAML");
387+
} catch (err) {
388+
if (err instanceof SlackError) {
389+
assert.include(
390+
err.message,
391+
"Invalid input! Failed to parse contents of the provided payload file",
392+
);
393+
assert.isDefined(err.cause?.values);
394+
assert.equal(err.cause.values.length, 1);
395+
assert.isTrue(err.cause.values[0] instanceof YAMLException);
396+
} else {
397+
assert.fail("Failed to throw a SlackError", err);
398+
}
399+
}
400+
});
352401
});
353402
});

0 commit comments

Comments
 (0)