Skip to content

Commit

Permalink
feat: ✨ Add the public readonly envVariable to the EnvNotSetError
Browse files Browse the repository at this point in the history
… based on the pre-existing constructor parameter
  • Loading branch information
pklaschka committed Oct 29, 2024
1 parent 3f81d3e commit 98bf3a4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class EnvNotSetError extends Error {
/**
* @param envVariable name of the environment variable
*/
constructor(envVariable: string, cause?: unknown) {
constructor(public readonly envVariable: string, cause?: unknown) {
super(
`Environment variable ${envVariable} is not set.\n` +
`You can also set it by specifying a path to a file ` +
Expand Down
54 changes: 47 additions & 7 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,57 @@ const REQUIRED_FAILING = {
Deno.test("EnvNotSetError", async (t) => {
await t.step("without cause", () => {
const err = new EnvNotSetError("TEST");
assertStringIncludes(err.message, "TEST");
assertStringIncludes(err.message, "TEST_FILE");
assertIsError(err);
assertStringIncludes(
err.message,
"TEST",
"Expected the message to contain the env variable name",
);
assertStringIncludes(
err.message,
"TEST_FILE",
"Expected the message to contain a note about setting the variable using TEST_FILE",
);
assertEquals(
err.envVariable,
"TEST",
"Expected the envVariable to be set correctly",
);
assertIsError(
err,
EnvNotSetError,
undefined,
"Expected the error to be an Error instance of EnvNotSetError",
);
});

await t.step("with cause", () => {
const err = new EnvNotSetError("TEST", new Error("cause"));
assertStringIncludes(err.message, "TEST");
assertStringIncludes(err.message, "TEST_FILE");
assertEquals(err.cause, new Error("cause"));
assertIsError(err);
assertStringIncludes(
err.message,
"TEST",
"Expected the message to contain the env variable name",
);
assertStringIncludes(
err.message,
"TEST_FILE",
"Expected the message to contain a note about setting the variable using TEST_FILE",
);
assertEquals(
err.envVariable,
"TEST",
"Expected the envVariable to be set correctly",
);
assertEquals(
err.cause,
new Error("cause"),
"Expected the cause to be set correctly",
);
assertIsError(
err,
EnvNotSetError,
undefined,
"Expected the error to be an Error instance of EnvNotSetError",
);
});
});

Expand Down

0 comments on commit 98bf3a4

Please sign in to comment.