Skip to content

Commit

Permalink
🎉 Feat(utils): add new Functionality! MyErrorCatcher,MyErrorWrapper,M…
Browse files Browse the repository at this point in the history
…yErrorHandler
  • Loading branch information
INeedJobToStartWork committed Feb 8, 2024
1 parent f2f9b2d commit 835ffc2
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 16 deletions.
11 changes: 10 additions & 1 deletion config/eslintrc/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,16 @@ export default [
"@typescript-eslint/triple-slash-reference": "error",
"@typescript-eslint/unified-signatures": "error",
"no-return-await": "off",
"require-await": "off"
"require-await": "off",
"@typescript-eslint/ban-types": [
"error",
{
types: {
TODO: "You have to do TODO before you can lint it!"
},
extendDefaults: true
}
]
}
}
];
6 changes: 1 addition & 5 deletions config/webpack/webpack.base.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import CopyPlugin from "copy-webpack-plugin";
import { resolve } from "path";

const __dirname = resolve();
Expand All @@ -25,10 +24,7 @@ export default {
filename: "[name].cjs",
path: resolve(__dirname, "lib"),
chunkLoading: false,
library: {
type: "commonjs2",
export: "default"
}
library: "index"
},
resolve: {
extensions: ["", ".ts", ".js", ".mjs", ".mts"],
Expand Down
23 changes: 22 additions & 1 deletion config/webpack/webpack.prod.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import webpackBaseConfig from "./webpack.base.js";
import bundle from "bundle-declarations-webpack-plugin";
import CopyPlugin from "copy-webpack-plugin";
import { resolve } from "path";
import { merge } from "webpack-merge";

Expand All @@ -16,5 +18,24 @@ export default merge(webpackBaseConfig, {
},
output: {
path: PATHOUT
}
},
plugins: [
new bundle.BundleDeclarationsWebpackPlugin({
entry: {
filePath: "./src/index.ts"
},
outFile: "index.d.ts",
compilationOptions: {},
removeEmptyLines: false,
removeEmptyExports: false
}),
new CopyPlugin({
patterns: [
{
from: resolve(__dirname, "package.json"),
to: PATHOUT
}
]
})
]
});
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { MyErrorCatcher, MyErrorWrapper } from "@/utils";
export type { TErrorReturned, TFunctionReturn, TMyError, TDataReturn, TMyErrorList } from "@/utils/types";
17 changes: 16 additions & 1 deletion src/utils/MyErrorCatcher/MyErrorCatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,26 @@ import type { Prettify, arrowFunction } from "@/utils/types";

type MyErrorCatcherReturn<T extends arrowFunction<T>> = Prettify<ReturnType<T>>;

// const MyErrorCatcher =
// <T extends (...args: any[]) => any>(fnThatMayThrow: T) =>
// (...args: Parameters<T>): Promise<Prettify<ReturnType<T>>> =>
// new Promise(resolve => {
// resolve(fnThatMayThrow(...args));
// });

// (...args: Parameters<T>) => ReturnType<T>

export const MyErrorCatcher =
<T extends arrowFunction<T>>(fnThatMayThrow: T) =>
async (...args: Parameters<T>): Promise<MyErrorCatcherReturn<T>> =>
async (...args: Parameters<T>): Promise<Prettify<ReturnType<T>>> =>
new Promise(resolve => {
resolve(fnThatMayThrow(...args));
});
// export const MyErrorCatcher =
// <T extends arrowFunction<T>>(fnThatMayThrow: T) =>
// async (...args: Parameters<T>): Promise<MyErrorCatcherReturn<T>> =>
// new Promise(resolve => {
// resolve(fnThatMayThrow(...args));
// });

export default MyErrorCatcher;
5 changes: 5 additions & 0 deletions src/utils/MyErrorHandler/MyErrorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const MyErrorHandler = <T extends keyof K, K extends Record<T, K[T]>>(errorName: T, errorSolutions: K) => {

Check failure on line 1 in src/utils/MyErrorHandler/MyErrorHandler.ts

View workflow job for this annotation

GitHub Actions / Continuous-Integration

'errorName' is declared but its value is never read.

Check failure on line 1 in src/utils/MyErrorHandler/MyErrorHandler.ts

View workflow job for this annotation

GitHub Actions / Continuous-Integration

'errorSolutions' is declared but its value is never read.
// if (errorName in errorSolutions) errorSolutions[errorName]();
};

export default MyErrorHandler;
1 change: 1 addition & 0 deletions src/utils/MyErrorHandler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./MyErrorHandler";
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./MyErrorCatcher";
export * from "./MyErrorWrapper";
export * from "./MyErrorHandler";
20 changes: 12 additions & 8 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ export type TODO = any; // eslint-disable-line @typescript-eslint/no-explicit-an
export type Prettify<T> = NonNullable<unknown> & {
[K in keyof T]: T[K];
};
export type arrowFunction<T extends (...args: Array<Parameters<T>>) => ReturnType<T>> = (
...args: Array<Parameters<T>>

export type arrowFunction<T extends (...args: Parameters<T>) => ReturnType<T>> = (
...args: Parameters<T>
) => ReturnType<T>;

export type ErrorTypesCatched =
Expand All @@ -17,14 +18,17 @@ export type ErrorTypesCatched =
export type TMyError = {
code?: number | string;
message?: {
user?: string | ((e: TODO) => unknown);
dev?: string | ((e: TODO) => unknown);
user?: string;
dev?: string;
};
hint?: {
user?: string | ((e: TODO) => unknown);
dev?: string | ((e: TODO) => unknown);
user?: string;
dev?: string;
};
};

export type TError = [TMyError, true];
export type TErrorProof<T> = TError | [T, false];
export type TDataReturn<T> = [T, false];
export type TErrorReturned = [TMyError, true];
export type TFunctionReturn<T> = Prettify<TDataReturn<T>> | Prettify<TErrorReturned>;

export type TMyErrorList = Record<string, TMyError>;

0 comments on commit 835ffc2

Please sign in to comment.