Skip to content

Commit e04059a

Browse files
♻️ Refactor(oh-my-error,EveryThingAbout)!: change myErrorWrapper return from tuple to object and update tutorial!
1 parent befb4d1 commit e04059a

10 files changed

Lines changed: 146 additions & 143 deletions

File tree

README.md

Lines changed: 72 additions & 69 deletions
Large diffs are not rendered by default.

packages/oh-my-error/README.md

Lines changed: 35 additions & 35 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { describe, expect, it } from "vitest";
2-
32
import { join } from "node:path";
43
import { myError, myErrorWrapper, myErrorHandler } from ".";
54
import { ErrorList as readErrorList, readFile } from "../tests/componentConcept/readFile";
65

76
describe("[FUNCTION] myErrorHandler", () => {
8-
it("ased", () => {
7+
it("should handle file not found error", () => {
98
const pathToFile = join(import.meta.dirname, "./OLOLOLOLOLO");
10-
const [data, isError] = myErrorWrapper(readFile)(pathToFile);
9+
const result = myErrorWrapper(readFile)(pathToFile);
1110

12-
expect(isError).toBe(true);
13-
expect(data).toEqual(myError(readErrorList.notFound, { hint: [pathToFile] }));
11+
expect(result.isError).toBe(true);
12+
expect(result.data).toEqual(myError(readErrorList.notFound, { hint: [pathToFile] }));
1413

1514
const MyErrorHandlerList = {
1615
FS001: () => {
@@ -19,8 +18,9 @@ describe("[FUNCTION] myErrorHandler", () => {
1918
}
2019
} as const;
2120

22-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
23-
const test = myErrorHandler((data as any).code, MyErrorHandlerList)();
24-
expect(test).toBe(true);
21+
//@ts-expect-error Expect error because there is no error narrowing
22+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
23+
const handled = myErrorHandler(result.data.code, MyErrorHandlerList)();
24+
expect(handled).toBe(true);
2525
});
2626
});

packages/oh-my-error/src/functions/myErrorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const ErrorList = {
3535
*
3636
* @example
3737
* ```
38-
* const [data, isError] = myErrorWrapper(readFile)("./ReadThisFile");
38+
* const {data, isError} = myErrorWrapper(readFile)("./ReadThisFile");
3939
*
4040
* const MyErrorHandlerList = {
4141
* FS001: () => {

packages/oh-my-error/src/functions/myErrorWrapper.test.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@ import type { TMyErrorList } from "../types";
66

77
describe("[FUNCTION] myErrorWrapper", () => {
88
describe("[ERROR]", () => {
9-
test("Double ()", () => {
9+
test("Double ()", async () => {
1010
const errorFunc = () => {
1111
throw new Error("AHA");
1212
};
1313

14-
const [data, isError] = myErrorWrapper(errorFunc)();
14+
const { data, isError } = await myErrorWrapper(errorFunc)();
1515
expect(isError).toEqual(true);
1616
expect(data).toEqual(new Error("AHA"));
1717
});
1818
test("Single () - PASSING ERROR", async () => {
1919
const errorFunc = () => {
2020
throw new Error("I throw Error!");
2121
};
22-
const [data, isError] = await myErrorWrapper(async () =>
22+
const { data, isError } = await myErrorWrapper(async () =>
2323
myErrorWrapper(errorFunc, (err: { message: string }) => myError({ cause: err.message }))()
2424
)();
2525
expect(isError).toBe(true);
2626
expect(data).toEqual({ cause: "I throw Error!" });
2727

2828
// expect().rejects.toThrow({ cause: "AHA" });
2929
});
30-
test("Single ()", () => {
30+
test("Single ()", async () => {
3131
const errorFunc = () => {
3232
throw new Error("AHA");
3333
};
3434

35-
const [data, isError] = myErrorWrapper(() => errorFunc())();
35+
const { data, isError } = await myErrorWrapper(() => errorFunc())();
3636
expect(isError).toEqual(true);
3737
expect(data).toEqual(new Error("AHA"));
3838
});
@@ -41,7 +41,7 @@ describe("[FUNCTION] myErrorWrapper", () => {
4141
throw new Error("AHA");
4242
};
4343

44-
const [data, isError] = await myErrorWrapper(errorFunc)();
44+
const { data, isError } = await myErrorWrapper(errorFunc)();
4545
expect(isError).toEqual(true);
4646
expect(data).toEqual(new Error("AHA"));
4747
});
@@ -50,7 +50,7 @@ describe("[FUNCTION] myErrorWrapper", () => {
5050
throw new Error("AHA");
5151
};
5252

53-
const [data, isError] = await myErrorWrapper(async () => errorFunc())();
53+
const { data, isError } = await myErrorWrapper(async () => errorFunc())();
5454
expect(isError).toEqual(true);
5555
expect(data).toEqual(new Error("AHA"));
5656
});
@@ -66,7 +66,7 @@ describe("[FUNCTION] myErrorWrapper", () => {
6666
if (sum1 > 10 || sum2 > 10) throw new Error("sum1 or sum2 is higher than 10!");
6767
return sum1 + sum2;
6868
};
69-
const [error, isError] = await myErrorWrapper(async () =>
69+
const { data: error, isError } = await myErrorWrapper(async () =>
7070
myErrorWrapper(sumMax10, myError(MyErrorList.TOO_HIGH_NUMBER))(1, 20)
7171
)();
7272
expect(isError).toEqual(true);
@@ -77,29 +77,29 @@ describe("[FUNCTION] myErrorWrapper", () => {
7777
test("Double ()", () => {
7878
const sum = (sum1: number, sum2: number) => sum1 + sum2;
7979

80-
const [data, isError] = myErrorWrapper(sum)(1, 2);
80+
const { data, isError } = myErrorWrapper(sum)(1, 2);
8181
expect(isError).toEqual(false);
8282
expect(data).toEqual(3);
8383
});
8484
test("Single ()", () => {
8585
const sum = (sum1: number, sum2: number) => sum1 + sum2;
8686

87-
// const [data, isError] = myErrorWrapper(() => sum)();
88-
const [data, isError] = myErrorWrapper(() => sum(1, 2))();
87+
// const {data, isError} = myErrorWrapper(() => sum)();
88+
const { data, isError } = myErrorWrapper(() => sum(1, 2))();
8989
expect(isError).toEqual(false);
9090
expect(data).toEqual(3);
9191
});
9292
test("Async Single ()", async () => {
9393
const sum = async (sum1: number, sum2: number) => (await sum1) + sum2;
9494

95-
const [data, isError] = await myErrorWrapper(async () => sum(1, 2))();
95+
const { data, isError } = await myErrorWrapper(async () => sum(1, 2))();
9696
expect(isError).toEqual(false);
9797
expect(data).toEqual(3);
9898
});
9999
test("Async Double ()", async () => {
100100
const sum = async (sum1: number, sum2: number) => (await sum1) + sum2;
101101

102-
const [data, isError] = await myErrorWrapper(sum)(1, 2);
102+
const { data, isError } = await myErrorWrapper(sum)(1, 2);
103103
expect(isError).toEqual(false);
104104
expect(data).toEqual(3);
105105
});

packages/oh-my-error/src/functions/myErrorWrapper.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ type AnyFunction = (...args: any[]) => any;
99

1010
type MyErrorWrapperReturn<Fn extends AnyFunction, ErrorType = undefined> = ErrorType extends undefined
1111
? TIsPromise<ReturnType<Fn>> extends true
12-
? Promise<[ErrorTypesCatched, true] | [UnwrapPromise<ReturnType<Fn>>, false]>
13-
: [ErrorTypesCatched, true] | [ReturnType<Fn>, false]
12+
? Promise<{ data: ErrorTypesCatched; isError: true } | { data: UnwrapPromise<ReturnType<Fn>>; isError: false }>
13+
: { data: ErrorTypesCatched; isError: true } | { data: ReturnType<Fn>; isError: false }
1414
: ReturnType<Fn>;
1515

1616
//----------------------
@@ -23,20 +23,20 @@ type MyErrorWrapperReturn<Fn extends AnyFunction, ErrorType = undefined> = Error
2323
* @param errorToThrow - If `fnThatMayThrow` throw error, this will be instant throwed.
2424
* - if `errorToThrow` is Function (cb), it pass as argument error catched by function else return element.
2525
* - typeof errorToThrow === "function" ? errorToThrow(error) : errorToThrow
26-
* @returns Tuple `[data,isError]`, with `errorToThrow` just `data`.
26+
* @returns Object `{ data, isError }`, with `errorToThrow` just `data`.
2727
*
2828
* @example
2929
* ```
3030
* // For Async add `await` before `myErrorWrapper`
31-
* const [data,isError] = myErrorWrapper(readFile)("path...");
31+
* const { data, isError } = myErrorWrapper(readFile)("path...");
3232
* if(isError) throw new Error("Can't read file!")
3333
*
3434
* // Or instant Error Throw (with errorToThrow)
3535
* const data = myErrorWrapper(readFile,new Error("Can't read file!"))("path...");
3636
* // With Passing Throwed Error to our error
3737
* const data = myErrorWrapper(readFile,err => new Error(`ERROR MESSAGE: ${err.message}}`))("path...");
3838
* ```
39-
* @version 2.0.0
39+
* @version 3.0.0
4040
*/
4141
export const myErrorWrapper =
4242
<Fn extends TFunction<Fn>, ErrorType = undefined>(
@@ -45,11 +45,11 @@ export const myErrorWrapper =
4545
): ((...args: Parameters<Fn>) => MyErrorWrapperReturn<Fn, ErrorType>) =>
4646
(...args: Parameters<Fn>): MyErrorWrapperReturn<Fn, ErrorType> => {
4747
const returnFNOutput = <G>(result: G) =>
48-
(errorToThrow ? result : [result, false]) as MyErrorWrapperReturn<Fn, ErrorType>;
48+
(errorToThrow ? result : { data: result, isError: false }) as MyErrorWrapperReturn<Fn, ErrorType>;
4949

5050
const returnError = (error: unknown) => {
5151
if (errorToThrow) throw typeof errorToThrow === "function" ? errorToThrow(error) : errorToThrow;
52-
return [error as ErrorTypesCatched, true] as MyErrorWrapperReturn<Fn, ErrorType>;
52+
return { data: error as ErrorTypesCatched, isError: true } as MyErrorWrapperReturn<Fn, ErrorType>;
5353
};
5454

5555
try {

packages/oh-my-error/src/tests/componentConcept/readFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("[FUNCTION] readFile", () => {
1414

1515
it('should throw a "Not Found" error when file does not exist', () => {
1616
const pathToFile = join(import.meta.dirname, "/oyyoyoyoyoyoyo");
17-
const [data, isError] = myErrorWrapper(readFile)(pathToFile);
17+
const { data, isError } = myErrorWrapper(readFile)(pathToFile);
1818
expect(isError).toEqual(true);
1919
expect(data).toEqual(myError(ErrorList.notFound, { hint: [pathToFile] }));
2020
});

tutorials/EveryThingAbout/src/myErrorCatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const data = new Promise((resolve) => {
2929
* 🟩 Solution 1: Basic Usage with Destructuring
3030
*
3131
* @description Simple Wrapping, no extra args.
32-
* @returns Array with two elements: [data, isError]
32+
* @returns Array with two elements: {data, isError}
3333
* - On Success: [functionResult, false]
3434
* - On Error: [caughtError, true]
3535
*/

tutorials/EveryThingAbout/src/myErrorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const MyErrorHandlerList = {
3232
},
3333
} as const;
3434

35-
const [data, isError] = myErrorWrapper(nameValidator)("badName");
35+
const { data, isError } = myErrorWrapper(nameValidator)("badName");
3636
if (isError) myErrorHandler((data as any).code, MyErrorHandlerList)();
3737
else console.log(data);
3838

tutorials/EveryThingAbout/src/myErrorWrapper.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* into elegant one-line expressions. It provides multiple ways to handle errors
99
* and access error information.
1010
*
11-
* @version 2.0.0
11+
* @version 3.0.0
1212
*/
1313

1414
import { myErrorWrapper } from "@packages/oh-my-error";
@@ -34,11 +34,11 @@ try {
3434
* 🟩 Solution 1: Basic Usage with Destructuring
3535
*
3636
* @description Simple Wrapping, no extra args.
37-
* @returns Array with two elements: [data, isError]
38-
* - On Success: [functionResult, false]
39-
* - On Error: [caughtError, true]
37+
* @returns Object with two elements: { data, isError }
38+
* - On Success: { data:functionResult, isError:false }
39+
* - On Error: { data:caughtError, isError:true }
4040
*/
41-
const [data, isError] = myErrorWrapper(crashIf10)(10);
41+
const { data, isError } = myErrorWrapper(crashIf10)(10);
4242
if (isError) throw new Error("Invalid number!");
4343
else console.log(data);
4444

@@ -72,9 +72,9 @@ try {
7272
* 🟩 Solution 1: Error Access with Destructuring
7373
*
7474
* @description Allows access to the original error through destructuring
75-
* @returns Array with [data, isError] where data contains the error on failure
75+
* @returns Object with {data, isError} where data contains the error on failure
7676
*/
77-
const [data3, isError2] = myErrorWrapper(crashIf10)(10);
77+
const { data3, isError2 } = myErrorWrapper(crashIf10)(10);
7878
if (isError2) throw new Error("Invalid number!", { cause: data3 });
7979
else console.log(data3);
8080

0 commit comments

Comments
 (0)