Skip to content

Commit aa3bd1c

Browse files
Merge pull request #13 from nakanoasaservice/feature/cause-check
Fix: preserve falsy cause values in TaggedError
2 parents ec624cd + 69c6abb commit aa3bd1c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.0.1",
2+
"version": "1.0.2",
33
"name": "@nakanoaas/tagged-error",
44
"license": "MIT",
55
"exports": "./index.ts",

index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ Deno.test("TaggedError - with cause data", () => {
3333
assertEquals(error.cause, causeData);
3434
});
3535

36+
Deno.test("TaggedError - should set falsy cause values (0/empty string/false/null)", () => {
37+
const errorWithZero = new TaggedError("CAUSE_ZERO", { cause: 0 });
38+
assertEquals(errorWithZero.cause, 0);
39+
40+
const errorWithEmptyString = new TaggedError("CAUSE_EMPTY_STRING", {
41+
cause: "",
42+
});
43+
assertEquals(errorWithEmptyString.cause, "");
44+
45+
const errorWithFalse = new TaggedError("CAUSE_FALSE", { cause: false });
46+
assertEquals(errorWithFalse.cause, false);
47+
48+
const errorWithNull = new TaggedError<"CAUSE_NULL", null>("CAUSE_NULL", {
49+
cause: null,
50+
});
51+
assertEquals(errorWithNull.cause, null);
52+
});
53+
54+
Deno.test("TaggedError - should not set cause when cause is undefined", () => {
55+
const error = new TaggedError("CAUSE_UNDEFINED", { cause: undefined });
56+
assertEquals(error.cause, undefined);
57+
});
58+
3659
describe("TaggedError in practice", () => {
3760
function divideAndSquareRoot(num: number, divisor: number) {
3861
if (divisor === 0) {

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class TaggedError<Tag extends string, Cause = undefined> extends Error {
6464
this.tag = tag;
6565
this.name = `TaggedError('${tag}')`;
6666

67-
if (options?.cause) {
67+
if (options?.cause !== undefined) {
6868
this.cause = options.cause;
6969
}
7070
}

0 commit comments

Comments
 (0)