Skip to content

Commit 143a262

Browse files
committed
style(core): apply consistent formatting across files and test cases
1 parent 8b73873 commit 143a262

13 files changed

Lines changed: 337 additions & 238 deletions

File tree

packages/core/__tests__/caller-trace.test.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// noinspection ES6UnusedImports
22
import { describe, expect, test } from "bun:test";
3-
import { That } from '@thaterror/core';
4-
import { ResultAsync } from 'neverthrow';
3+
import { That } from "@thaterror/core";
4+
import { ResultAsync } from "neverthrow";
55

66
describe("ThatError Location Anchoring", () => {
77
const AppError = That({
88
SYNC_ERR: "Sync failure",
99
ASYNC_ERR: (url: string) => `Async failure: ${url}`,
10-
LOC_ERR: "Location test error"
10+
LOC_ERR: "Location test error",
1111
});
1212

13-
const pathSegments = new URL(import.meta.url).pathname.split('/');
13+
const pathSegments = new URL(import.meta.url).pathname.split("/");
1414
const currentFileName = pathSegments.at(-1) ?? "";
1515

1616
// --- Scenario 1: The Native Way (Messy) ---
@@ -24,7 +24,7 @@ describe("ThatError Location Anchoring", () => {
2424
* A standard 'new Error' is captured here, but the stack trace
2525
* will be cluttered with neverthrow's internal dispatcher frames.
2626
*/
27-
new Error(`Native wrap: ${error}`)
27+
new Error(`Native wrap: ${error}`),
2828
).andThen(() => {
2929
throw new Error("Should not happen");
3030
});
@@ -71,7 +71,7 @@ describe("ThatError Location Anchoring", () => {
7171
// The engine captures the stack during 'new', but without the
7272
// explicit anchor, the trace quality depends purely on V8's mood.
7373
return AppError.LOC_ERR();
74-
}
74+
},
7575
);
7676

7777
if (result.isErr()) {
@@ -84,29 +84,27 @@ describe("ThatError Location Anchoring", () => {
8484

8585
// --- Scenario 4: Synchronous Business Logic ---
8686
test("Sync: stack trace should anchor at the business caller via .at()", () => {
87-
function businessFunction() {
88-
/**
89-
* 💡 THE ANCHOR POINT
90-
* We call .at() here to explicitly mark this line as the "Crime Scene".
91-
*/
92-
return AppError.SYNC_ERR().with(void 0);
93-
}
87+
function businessFunction() {
88+
/**
89+
* 💡 THE ANCHOR POINT
90+
* We call .at() here to explicitly mark this line as the "Crime Scene".
91+
*/
92+
return AppError.SYNC_ERR().with(void 0);
93+
}
9494

95-
const err = businessFunction();
96-
const stack = err.stack ?? "";
97-
const topFrame = stack.split("\n")[1];
95+
const err = businessFunction();
96+
const stack = err.stack ?? "";
97+
const topFrame = stack.split("\n")[1];
9898

99-
// 🎯 Verification: The first frame must point to 'businessFunction' in THIS file.
100-
expect(topFrame).toContain("businessFunction");
101-
expect(topFrame).toContain(currentFileName);
99+
// 🎯 Verification: The first frame must point to 'businessFunction' in THIS file.
100+
expect(topFrame).toContain("businessFunction");
101+
expect(topFrame).toContain(currentFileName);
102102

103-
// 🛡️ Noise Removal: The factory internals (define.ts) must be sliced off.
104-
expect(topFrame).not.toContain("define.ts");
103+
// 🛡️ Noise Removal: The factory internals (define.ts) must be sliced off.
104+
expect(topFrame).not.toContain("define.ts");
105105

106-
console.log(stack.split("\n").slice(0, 4).join("\n"));
107-
}
108-
)
109-
;
106+
console.log(stack.split("\n").slice(0, 4).join("\n"));
107+
});
110108

111109
// --- Scenario 5: Neverthrow Async Callback ---
112110
test("Async: stack trace should anchor inside neverthrow callback via .at()", async () => {
@@ -122,8 +120,8 @@ describe("ThatError Location Anchoring", () => {
122120
* By calling .at() inside this anonymous closure, we lock the stack
123121
* to this exact line in the business logic.
124122
*/
125-
return AppError.ASYNC_ERR(url).with({cause: error});
126-
}
123+
return AppError.ASYNC_ERR(url).with({ cause: error });
124+
},
127125
);
128126

129127
if (result.isErr()) {
@@ -152,4 +150,4 @@ describe("ThatError Location Anchoring", () => {
152150
throw new Error("Test failed: Result should be an Err");
153151
}
154152
});
155-
});
153+
});

packages/core/__tests__/define-error.test.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,35 @@ import {
88
ScopeField,
99
scopeOf,
1010
That,
11-
} from "@thaterror/core"
11+
} from "@thaterror/core";
1212

1313
export const AppError = That({
1414
NotFound: (id: number) => `Resource ${id} not found`,
1515
Unauthorized: "User is not logged in",
1616
DatabaseError: (query: string) => `Query failed: ${query}`,
17-
ShardError: (characterId: number, shard: string) => `Character & shard unmatch ${characterId} : ${shard}`
17+
ShardError: (characterId: number, shard: string) =>
18+
`Character & shard unmatch ${characterId} : ${shard}`,
1819
});
1920

2021
export type AppErrorUnion = ErrorUnionOf<typeof AppError>;
2122

2223
describe("defineError strict type testing", () => {
23-
2424
test("should be correctly scoped", () => {
2525
const err = AppError.Unauthorized();
2626
expect(scopeOf(AppError)).toBe(scopeOf(err));
2727
expect(scopeOf(AppError)).toBe(scopeOf(AppError.ShardError));
2828
expect(scopeOf(AppError)).toBe(scopeOf(AppError.NotFound));
2929
expect(scopeOf(AppError)).toBe(scopeOf(AppError.Unauthorized));
30-
})
30+
});
3131

3232
test("static error should be correctly generated", () => {
3333
const err = AppError.Unauthorized();
3434

3535
if (isDefinedError(err, scopeOf(AppError))) {
3636
expect(isDefinedError(err, scopeOf(AppError))).toBe(true);
37-
switch ((err as AppErrorUnion)[CodeField]) { // type auto infer
37+
switch (
38+
(err as AppErrorUnion)[CodeField] // type auto infer
39+
) {
3840
case "DatabaseError":
3941
expect(false).toBe(true);
4042
throw new Error("Should not happen");
@@ -44,7 +46,6 @@ describe("defineError strict type testing", () => {
4446
case "Unauthorized":
4547
break;
4648
}
47-
4849
}
4950

5051
expect(err[CodeField]).toBe("Unauthorized");
@@ -58,7 +59,7 @@ describe("defineError strict type testing", () => {
5859
expect(err[ScopeField]).toBe(scopeOf(AppError));
5960
expect(isDefinedError(err, scopeOf(AppError))).toBe(true);
6061
expect(isDefinedError(err, Symbol())).toBe(false);
61-
})
62+
});
6263

6364
test("error with parameters should correctly capture payload", () => {
6465
const err = AppError.NotFound(404);
@@ -72,12 +73,14 @@ describe("defineError strict type testing", () => {
7273

7374
test("should support native Error.cause (#[source])", () => {
7475
const original = new Error("Connection lost");
75-
const err = AppError.Unauthorized().with({cause: original});
76+
const err = AppError.Unauthorized().with({ cause: original });
7677

7778
expect(err.cause).toBe(original);
7879

7980
const shardOriginal = new Error("Shard not found");
80-
const shardErr = AppError.ShardError(1, "shard-1").with({cause: shardOriginal});
81+
const shardErr = AppError.ShardError(1, "shard-1").with({
82+
cause: shardOriginal,
83+
});
8184
expect(shardErr.cause).toBe(shardOriginal);
8285
});
8386

@@ -96,7 +99,6 @@ describe("defineError strict type testing", () => {
9699
try {
97100
fn();
98101
} catch (e: unknown) {
99-
100102
const isError = e instanceof Error;
101103
expect(isError).toBe(true);
102104

@@ -118,4 +120,4 @@ describe("defineError strict type testing", () => {
118120
expect(err[PayloadField]).toEqual([1]);
119121
}
120122
});
121-
});
123+
});
Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, expect, test } from 'bun:test'
2-
import { HTTPException } from 'hono/http-exception'
3-
import { AppError } from './define-error.test'
1+
import { describe, expect, test } from "bun:test";
2+
import { HTTPException } from "hono/http-exception";
3+
import { AppError } from "./define-error.test";
44

55
class MyCustomError extends Error {
66
prop = 1;
@@ -12,52 +12,60 @@ class MyCustomError2 extends Error {
1212
}
1313
}
1414

15-
16-
describe('ErrorFamily from native error testing', () => {
15+
describe("ErrorFamily from native error testing", () => {
1716
const HttpBridgedError = AppError.bridge(HTTPException, (e, cases) => {
1817
return e.status === 404
1918
? cases.NotFound(Number(e.message) || 0)
2019
: e.status === 401
21-
? cases.Unauthorized()
22-
: e.status === 500
23-
? cases.DatabaseError(e.message)
24-
: cases.ShardError(0, "FORBIDDEN_SHARD")
25-
26-
})
27-
28-
test('should chain up error family', () => {
29-
const ExAppError = AppError.enroll(MyCustomError, AppError.Unauthorized);
20+
? cases.Unauthorized()
21+
: e.status === 500
22+
? cases.DatabaseError(e.message)
23+
: cases.ShardError(0, "FORBIDDEN_SHARD");
24+
});
25+
26+
test("should chain up error family", () => {
27+
const ExAppError = AppError.enroll(
28+
MyCustomError,
29+
AppError.Unauthorized,
30+
);
3031
const InferErr = ExAppError.from(new MyCustomError());
3132
expect(InferErr.is(AppError.Unauthorized)).toBe(true);
3233

33-
const ExxAppError = ExAppError.enroll(HTTPException, AppError.NotFound, (e) => [e.status]);
34-
expect(ExxAppError.from(new HTTPException(404)).is(AppError.NotFound)).toBe(true);
35-
36-
37-
const ExrAppError = ExxAppError
38-
.enroll(MyCustomError, AppError.NotFound, (e) => [e.prop])
39-
.enroll(SyntaxError, AppError.ShardError, (_e) => [2, 'xx']);
40-
41-
expect(ExrAppError.from(new MyCustomError()).is(AppError.NotFound)).toBe(true)
34+
const ExxAppError = ExAppError.enroll(
35+
HTTPException,
36+
AppError.NotFound,
37+
(e) => [e.status],
38+
);
39+
expect(
40+
ExxAppError.from(new HTTPException(404)).is(AppError.NotFound),
41+
).toBe(true);
42+
43+
const ExrAppError = ExxAppError.enroll(
44+
MyCustomError,
45+
AppError.NotFound,
46+
(e) => [e.prop],
47+
).enroll(SyntaxError, AppError.ShardError, (_e) => [2, "xx"]);
48+
49+
expect(
50+
ExrAppError.from(new MyCustomError()).is(AppError.NotFound),
51+
).toBe(true);
4252

4353
const unenrolledError = new MyCustomError2();
4454
// @ts-expect-error
45-
expect(() => ExrAppError.from(unenrolledError)).toThrowError(Error)
46-
})
47-
48-
test('should bridge error correctly', () => {
49-
55+
expect(() => ExrAppError.from(unenrolledError)).toThrowError(Error);
56+
});
5057

51-
const nfErr = HttpBridgedError.from(new HTTPException(404))
58+
test("should bridge error correctly", () => {
59+
const nfErr = HttpBridgedError.from(new HTTPException(404));
5260
expect(nfErr.is(AppError.NotFound)).toBe(true);
53-
})
61+
});
5462

55-
test('should handle cause correctly', () => {
63+
test("should handle cause correctly", () => {
5664
const customError = new MyCustomError();
5765
const GenError = AppError.enroll(MyCustomError, AppError.Unauthorized);
5866
expect(GenError.from(customError).cause).toBe(customError);
5967

6068
const not_found = new HTTPException(404);
6169
expect(HttpBridgedError.from(not_found).cause).toBe(not_found);
62-
})
63-
})
70+
});
71+
});
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
// noinspection ES6UnusedImports
22
import { describe, expect, test } from "bun:test";
3-
import { codeOf, PayloadField, payloadOf, type ThatError } from "@thaterror/core";
4-
import { err, fromPromise } from 'neverthrow'
5-
import { AppError } from './define-error.test'
6-
7-
describe('neverthrow test', () => {
8-
test('should infer Error type', () => {
3+
import {
4+
codeOf,
5+
PayloadField,
6+
payloadOf,
7+
type ThatError,
8+
} from "@thaterror/core";
9+
import { err, fromPromise } from "neverthrow";
10+
import { AppError } from "./define-error.test";
11+
12+
describe("neverthrow test", () => {
13+
test("should infer Error type", () => {
914
const getNotFoundResult = () => {
1015
const e = AppError.NotFound(404);
1116
return err<string, ThatError<typeof AppError, "NotFound">>(e);
@@ -20,16 +25,15 @@ describe('neverthrow test', () => {
2025
expect(error.is(AppError.NotFound)).toBe(true);
2126
expect(error[PayloadField]).toEqual([404]);
2227
}
23-
})
28+
});
2429

25-
test('should infer Error type from async call', async () => {
30+
test("should infer Error type from async call", async () => {
2631
const id = 123;
2732
const fetchData = () => {
28-
const promise = Promise.reject(new Error('fetchData'));
33+
const promise = Promise.reject(new Error("fetchData"));
2934

30-
return fromPromise(
31-
promise,
32-
(e) => AppError.NotFound(id).with({cause: e})
35+
return fromPromise(promise, (e) =>
36+
AppError.NotFound(id).with({ cause: e }),
3337
);
3438
};
3539

@@ -44,5 +48,5 @@ describe('neverthrow test', () => {
4448
const code = codeOf(result.error);
4549
expect(code).toBe("NotFound");
4650
}
47-
})
48-
})
51+
});
52+
});

0 commit comments

Comments
 (0)