@herrlich-digital/trycatch-wrapper 1.0.4
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/README.md +114 -0
- package/dist/index.cjs +38 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# TryCatch-Wrapper
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
TryCatch-Wrapper is a lightweight utility designed to simplify error handling in TypeScript applications. It provides a structured approach to handling asynchronous errors using a `Result` type pattern, ensuring better code maintainability and readability.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
You can install the package from GitHub Packages using bun:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
bun add @pr0gstar/trycatch-wrapper
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
or using npm:
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm install @pr0gstar/trycatch-wrapper
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
or using yarn:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
yarn add @pr0gstar/trycatch-wrapper
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
### API
|
|
32
|
+
|
|
33
|
+
The core function of this package is:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
type Success<T> = {
|
|
37
|
+
data: T;
|
|
38
|
+
error: null;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
type Failure<E> = {
|
|
42
|
+
data: null;
|
|
43
|
+
error: E;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
47
|
+
|
|
48
|
+
export async function tryCatch<T, E = Error>(
|
|
49
|
+
promise: Promise<T>,
|
|
50
|
+
): Promise<Result<T, E>> {
|
|
51
|
+
try {
|
|
52
|
+
const data = await promise;
|
|
53
|
+
return { data, error: null };
|
|
54
|
+
} catch (error) {
|
|
55
|
+
return { data: null, error: error as E };
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Basic Example
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
import { tryCatch } from "@pr0gstar/trycatch-wrapper";
|
|
64
|
+
|
|
65
|
+
async function fetchData(): Promise<string> {
|
|
66
|
+
return await fetch("https://api.example.com/data").then((res) => res.json());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
(async () => {
|
|
70
|
+
const { data, error } = await tryCatch(fetchData());
|
|
71
|
+
if (error) throw error;
|
|
72
|
+
console.log("Fetched data:", data);
|
|
73
|
+
})();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Handling Different Error Types
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
interface CustomError {
|
|
80
|
+
message: string;
|
|
81
|
+
code: number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const { data, error } = await tryCatch<string, CustomError>(
|
|
85
|
+
Promise.reject({ message: "Something went wrong", code: 500 }),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
if (error) throw new Error(`Error ${error.code}: ${error.message}`);
|
|
89
|
+
|
|
90
|
+
console.log("Success:", data);
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Benefits
|
|
94
|
+
|
|
95
|
+
- **Type Safety**: Ensures that both success and failure cases are handled properly.
|
|
96
|
+
- **Avoids Try-Catch Blocks Everywhere**: Simplifies error handling for async operations.
|
|
97
|
+
- **Structured Error Handling**: Uses a `Result<T, E>` type pattern for better maintainability.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
This project is licensed under the MIT License.
|
|
102
|
+
|
|
103
|
+
## Contributing
|
|
104
|
+
|
|
105
|
+
Contributions are welcome! Please open an issue or submit a pull request.
|
|
106
|
+
|
|
107
|
+
## Author
|
|
108
|
+
|
|
109
|
+
Developed by Christoph Planken.
|
|
110
|
+
|
|
111
|
+
## Links
|
|
112
|
+
|
|
113
|
+
- [GitHub Repository](https://github.com/pr0gstar/trycatch-wrapper)
|
|
114
|
+
- [GitHub Packages](https://github.com/pr0gstar?tab=packages)
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
tryCatch: () => tryCatch
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
async function tryCatch(promise) {
|
|
27
|
+
try {
|
|
28
|
+
const data = await promise;
|
|
29
|
+
return { data, error: null };
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return { data: null, error };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
35
|
+
0 && (module.exports = {
|
|
36
|
+
tryCatch
|
|
37
|
+
});
|
|
38
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["type Success<T> = {\n data: T;\n error: null;\n};\n\ntype Failure<E> = {\n data: null;\n error: E;\n};\n\ntype Result<T, E = Error> = Success<T> | Failure<E>;\n\nexport async function tryCatch<T, E = Error>(\n promise: Promise<T>,\n): Promise<Result<T, E>> {\n try {\n const data = await promise;\n return { data, error: null };\n } catch (error) {\n return { data: null, error: error as E };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAYA,eAAsB,SACpB,SACuB;AACvB,MAAI;AACF,UAAM,OAAO,MAAM;AACnB,WAAO,EAAE,MAAM,OAAO,KAAK;AAAA,EAC7B,SAAS,OAAO;AACd,WAAO,EAAE,MAAM,MAAM,MAAkB;AAAA,EACzC;AACF;","names":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Success<T> = {
|
|
2
|
+
data: T;
|
|
3
|
+
error: null;
|
|
4
|
+
};
|
|
5
|
+
type Failure<E> = {
|
|
6
|
+
data: null;
|
|
7
|
+
error: E;
|
|
8
|
+
};
|
|
9
|
+
type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
10
|
+
declare function tryCatch<T, E = Error>(promise: Promise<T>): Promise<Result<T, E>>;
|
|
11
|
+
|
|
12
|
+
export { tryCatch };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Success<T> = {
|
|
2
|
+
data: T;
|
|
3
|
+
error: null;
|
|
4
|
+
};
|
|
5
|
+
type Failure<E> = {
|
|
6
|
+
data: null;
|
|
7
|
+
error: E;
|
|
8
|
+
};
|
|
9
|
+
type Result<T, E = Error> = Success<T> | Failure<E>;
|
|
10
|
+
declare function tryCatch<T, E = Error>(promise: Promise<T>): Promise<Result<T, E>>;
|
|
11
|
+
|
|
12
|
+
export { tryCatch };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../index.ts"],"sourcesContent":["type Success<T> = {\n data: T;\n error: null;\n};\n\ntype Failure<E> = {\n data: null;\n error: E;\n};\n\ntype Result<T, E = Error> = Success<T> | Failure<E>;\n\nexport async function tryCatch<T, E = Error>(\n promise: Promise<T>,\n): Promise<Result<T, E>> {\n try {\n const data = await promise;\n return { data, error: null };\n } catch (error) {\n return { data: null, error: error as E };\n }\n}\n"],"mappings":";AAYA,eAAsB,SACpB,SACuB;AACvB,MAAI;AACF,UAAM,OAAO,MAAM;AACnB,WAAO,EAAE,MAAM,OAAO,KAAK;AAAA,EAC7B,SAAS,OAAO;AACd,WAAO,EAAE,MAAM,MAAM,MAAkB;AAAA,EACzC;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@herrlich-digital/trycatch-wrapper",
|
|
3
|
+
"description": "A simple try-catch wrapper for TypeScript inspired by Golang",
|
|
4
|
+
"version": "1.0.4",
|
|
5
|
+
"entrypoint": "index.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.cjs",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./dist/index.cjs"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "tsup",
|
|
19
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/bun": "latest",
|
|
23
|
+
"tsup": "^8.4.0"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"typescript": "^5"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"try",
|
|
30
|
+
"catch",
|
|
31
|
+
"trycatch",
|
|
32
|
+
"wrapper",
|
|
33
|
+
"typescript"
|
|
34
|
+
],
|
|
35
|
+
"author": "Christoph Planken <Herrlich Digital>",
|
|
36
|
+
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "https://github.com/Herrlich-Digital/tryCatch-Wrapper.git"
|
|
40
|
+
}
|
|
41
|
+
}
|