Skip to content

Commit

Permalink
🎉 Feat(myErrorWrapper,errors.ts)!: Add error passing to `throwedError…
Browse files Browse the repository at this point in the history
…` at `myErrorWrapper` and add some types with TSDOCS
  • Loading branch information
INeedJobToStartWork committed Nov 20, 2024
1 parent ee814eb commit b9d8ffa
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 117 deletions.
7 changes: 7 additions & 0 deletions .changeset/honest-jeans-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"oh-my-error": minor
---

- Add passing Throwed Error to our error at `myErrorWrapper`
- Add types `ValueOrFunctionAll` and `ValueOrFunction`
- Add missing TSDocs to types
4 changes: 2 additions & 2 deletions .changeset/pre.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"mode": "pre",
"tag": "prerelease.2",
"tag": "prerelease",
"initialVersions": {
"oh-my-error": "2.0.0-prerelease.1"
},
"changesets": ["short-eels-sparkle"]
"changesets": ["honest-jeans-own", "short-eels-sparkle"]
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# oh-my-error

## 2.0.0-prerelease.3

### Minor Changes

- Add passing Throwed Error to our error at `myErrorWrapper`
- Add types `ValueOrFunctionAll` and `ValueOrFunction`
- Add missing TSDocs to types

## 2.0.0-prerelease.2

### Patch Changes
Expand Down
56 changes: 38 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![image](https://github.com/INeedJobToStartWork/MyError/assets/97305201/03fa3e50-af28-4345-a3f7-f84d091b4eb1)

<h1 align="center">MyError</h1>
<p align="center"><code>🎉2.0.0.prerelease.2🎊</code></p>
<p align="center"><code>🎉2.0.0.prerelease.3🎊</code></p>
<p align="center"><b>A Very Clean Error Handler!</b></p>
<div align="center">
<a >đź“—Tutorials (in soon)</a> |
Expand Down Expand Up @@ -58,8 +58,10 @@ const data = myErrorWrapper(readFile, Error("Cant Load File!"))("...path");
- [myErrorHandler](#myerrorhandler)
- [Types](#types-1)
- [TMyErrorList](#tmyerrorlist)
- [Error Templates (Interfaces) `new!`](#error-templates-interfaces-new)
- [Predefined elements for Functions (Atoms) `new!`](#predefined-elements-for-functions-atoms-new)
- [Error Templates (Organisms) `new!`](#error-templates-organisms-new)
- [Predefined elements (Moleculars) `new!`](#predefined-elements-moleculars-new)
- [Predefined types for properties (Atoms) `new!`](#predefined-types-for-properties-atoms-new)
- [Utils Types `new!`](#utils-types--new)

## Install

Expand Down Expand Up @@ -151,11 +153,11 @@ try {

const [data,isError] = myErrorWrapper(readFile)("path...");
if(isError) throw new Error("Can't read file!")

// Or instant Error Throw


// Or instant Error Throw (with errorToThrow)
const data = myErrorWrapper(readFile,new Error("Can't read file!"))("path...");

// With Passing Throwed Error to our error
const data = myErrorWrapper(readFile,err => new Error(`ERROR MESSAGE: ${err.message}}`))("path...");
```

> [!TIP] Async Functions
Expand Down Expand Up @@ -225,25 +227,43 @@ const ErrorList = {
} as const satisfies TMyErrorList;
```

### Error Templates (Interfaces) `new!`
### Error Templates (Organisms) `new!`

There you can find ready error structures.

| Name | Description | Extends |
| --------------------------------------------------------------------------------------------------------- | --------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [IMyError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Basic Error | - |
| [IMyErrorAPI](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Basic Error for an API error | [IMyError](#error-templates-interfaces), [TApiError](#predefined-elements-for-functions-atoms) |
| [IMyErrorRateLimit](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | API error for RateLimit | [IMyError](#error-templates-interfaces), [TApiRateLimit](#predefined-elements-for-functions-atoms) |
| [IMyErrorValidation](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | API error for Validation problems | [IMyError](#error-templates-interfaces), [TValidationError](#predefined-elements-for-functions-atoms) |
| [IMyError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Basic Error Template for **Error** | |
| [IMyErrorAPI](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Basic Error Template for **API** | [IMyError](#error-templates-interfaces), [TApiError](#predefined-elements-for-functions-atoms) |
| [IMyErrorRateLimit](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Basic Error Template for **API Rate limit** | [IMyError](#error-templates-interfaces), [TApiRateLimit](#predefined-elements-for-functions-atoms) |
| [IMyErrorValidation](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Basic Error Template for **Validation** | [IMyError](#error-templates-interfaces), [TValidationError](#predefined-elements-for-functions-atoms) |
| [TAllMyErrorTypes](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Every Error Template **exclude** `TBaseError` & `TBaseErrorExt` | [IMyError](#error-templates-interfaces) \| [IMyErrorAPI](#error-templates-interfaces) \| [IMyErrorRateLimit](#error-templates-interfaces) \| [IMyErrorValidation](#error-templates-interfaces) |
| [TAllMyErrorTypesExt](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Every Error Template with return type Function or Value option **exclude** `TBaseError` & `TBaseErrorExt` | [ValueOrFunction]()<[TAllMyErrorTypes](#error-templates-interfaces)> |
| | | |
| [TBaseError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Predefined type for [**Error**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) | - |
| [TBaseErrorExt](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | Predefined type for [**Error**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error) with function types | - |


### Predefined elements for Functions (Atoms) `new!`
### Predefined elements (Moleculars) `new!`

Short predefined types to easy creating own Error types!

| Name (Col1) | Name (Col2) | Name (Col3) |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [TSeverity](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TSeverity2](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TErrorMessages](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` |
| [TErrorMessagesExt](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TMyErrorList](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TErrorList](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` |
| [TCauseError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TDetails](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TBaseError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` |
| [TBaseErrorExt](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TValidationError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TApiError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` |
| [TApiRateLimit](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [StatusCodes](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/statusCodes.ts)`new!` | |
| [TValidationError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TApiError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TApiRateLimit](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` |
| [TErrorMessagesExt](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TErrorList](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TCauseError](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` |
| [TDetails](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | - | -|

### Predefined types for properties (Atoms) `new!`

Short predefined types for properties!

| Name (Col1) | Name (Col2) | Name (Col3) |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [TSeverity](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [TSeverity2](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [StatusCodes](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/statusCodes.ts) |

### Utils Types `new!`

| Name (Col1) | Name (Col2) | Name (Col3) |
| -------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [ValueOrFunction](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | [ValueOrFunctionAll](https://github.com/INeedJobToStartWork/MyError/blob/main/src/types/errors.ts) `new!` | - |
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oh-my-error",
"version": "2.0.0-prerelease.2",
"version": "2.0.0-prerelease.2.3",
"description": "A simple error handler for nodejs",
"keywords": [
"error",
Expand Down
12 changes: 12 additions & 0 deletions src/functions/myErrorWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ describe("[FUNCTION] myErrorWrapper", () => {
expect(isError).toEqual(true);
expect(data).toEqual(new Error("AHA"));
});
test("Single () - PASSING ERROR", async () => {
const errorFunc = () => {
throw new Error("I throw Error!");
};
const [data, isError] = await myErrorWrapper(async () =>
myErrorWrapper(errorFunc, (err: { message: string }) => myError({ cause: err.message }))()
)();
expect(isError).toBe(true);
expect(data).toEqual({ cause: "I throw Error!" });

// expect().rejects.toThrow({ cause: "AHA" });
});
test("Single ()", () => {
const errorFunc = () => {
throw new Error("AHA");
Expand Down
8 changes: 5 additions & 3 deletions src/functions/myErrorWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type MyErrorWrapperReturn<Fn extends AnyFunction, ErrorType = undefined> = Error
* TryCatch One line wrapper.
*
* @params errorToThrow - If `fnThatMayThrow` throw error, this will be instant throwed.
* - if `errorToThrow` is Function (cb), it pass as argument error catched by function else return element.
* - typeof errorToThrow === "function" ? errorToThrow(error) : errorToThrow
* @returns Tuple `[data,isError]`, with `errorToThrow` just `data`.
*
* @example
Expand All @@ -29,10 +31,10 @@ type MyErrorWrapperReturn<Fn extends AnyFunction, ErrorType = undefined> = Error
* const [data,isError] = myErrorWrapper(readFile)("path...");
* if(isError) throw new Error("Can't read file!")
*
*
* // Or instant Error Throw (with errorToThrow)
*
* const data = myErrorWrapper(readFile,new Error("Can't read file!"))("path...");
* // With Passing Throwed Error to our error
* const data = myErrorWrapper(readFile,err => new Error(`ERROR MESSAGE: ${err.message}}`))("path...");
* ```
*/
export const myErrorWrapper =
Expand All @@ -45,7 +47,7 @@ export const myErrorWrapper =
(errorToThrow ? result : [result, false]) as MyErrorWrapperReturn<Fn, ErrorType>;

const returnError = (error: unknown) => {
if (errorToThrow) throw errorToThrow;
if (errorToThrow) throw typeof errorToThrow === "function" ? errorToThrow(error) : errorToThrow;
return [error as ErrorTypesCatched, true] as MyErrorWrapperReturn<Fn, ErrorType>;
};

Expand Down
Loading

0 comments on commit b9d8ffa

Please sign in to comment.