emessages 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +11 -0
- package/dist/index.js +23 -0
- package/package.json +25 -0
- package/src/index.ts +37 -0
- package/tsconfig.json +11 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type ErrorMap = Record<string, string>;
|
|
2
|
+
export declare class ShowE extends Error {
|
|
3
|
+
code: string;
|
|
4
|
+
constructor(code: string);
|
|
5
|
+
}
|
|
6
|
+
declare global {
|
|
7
|
+
interface Function {
|
|
8
|
+
Emessage(map: ErrorMap): (...args: any[]) => Promise<any>;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export class ShowE extends Error {
|
|
2
|
+
constructor(code) {
|
|
3
|
+
super(code);
|
|
4
|
+
this.code = code;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
const wrapWithHandler = (func, messages) => {
|
|
8
|
+
return async (...args) => {
|
|
9
|
+
var _a, _b;
|
|
10
|
+
try {
|
|
11
|
+
return await func(...args);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
const errorCode = (_a = error === null || error === void 0 ? void 0 : error.code) !== null && _a !== void 0 ? _a : "UNKNOWN_ERROR";
|
|
15
|
+
const message = (_b = messages[errorCode]) !== null && _b !== void 0 ? _b : "Unexpected error occurred";
|
|
16
|
+
console.error(`[${func.name || "anonymous"}] ${message}`);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
// 🔥 Prototype extension — runs when module is imported
|
|
21
|
+
Function.prototype.Emessage = function (map) {
|
|
22
|
+
return wrapWithHandler(this, map);
|
|
23
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "emessages",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Function prototype based error handler with code-to-message mapping",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
"sideEffects": true,
|
|
17
|
+
|
|
18
|
+
"scripts": {
|
|
19
|
+
"build": "tsc"
|
|
20
|
+
},
|
|
21
|
+
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"typescript": "^5.9.3"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type ErrorMap = Record<string, string>;
|
|
2
|
+
|
|
3
|
+
export class ShowE extends Error {
|
|
4
|
+
code: string;
|
|
5
|
+
|
|
6
|
+
constructor(code: string) {
|
|
7
|
+
super(code);
|
|
8
|
+
this.code = code;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const wrapWithHandler = (func: Function, messages: ErrorMap) => {
|
|
13
|
+
return async (...args: any[]) => {
|
|
14
|
+
try {
|
|
15
|
+
return await func(...args);
|
|
16
|
+
} catch (error: any) {
|
|
17
|
+
const errorCode = error?.code ?? "UNKNOWN_ERROR";
|
|
18
|
+
const message = messages[errorCode] ?? "Unexpected error occurred";
|
|
19
|
+
console.error(`[${func.name || "anonymous"}] ${message}`);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// 🌍 Global augmentation
|
|
25
|
+
declare global {
|
|
26
|
+
interface Function {
|
|
27
|
+
Emessage(map: ErrorMap): (...args: any[]) => Promise<any>;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 🔥 Prototype extension — runs when module is imported
|
|
32
|
+
Function.prototype.Emessage = function (map: ErrorMap) {
|
|
33
|
+
return wrapWithHandler(this, map);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Required for global augmentation
|
|
37
|
+
export {};
|