safe-try-with-ai 1.3.1 → 1.3.2
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 +139 -19
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
---
|
|
2
|
+
|
|
1
3
|
# safe-try-with-ai
|
|
2
4
|
|
|
3
5
|
A lightweight JavaScript utility for **clean error handling** with optional **AI-style runtime suggestions**, without repetitive try/catch blocks.
|
|
@@ -10,6 +12,8 @@ A lightweight JavaScript utility for **clean error handling** with optional **AI
|
|
|
10
12
|
npm install safe-try-with-ai
|
|
11
13
|
```
|
|
12
14
|
|
|
15
|
+
---
|
|
16
|
+
|
|
13
17
|
## Usage
|
|
14
18
|
|
|
15
19
|
### Synchronous example
|
|
@@ -20,51 +24,167 @@ const { safeTry } = require("safe-try-with-ai");
|
|
|
20
24
|
const [err, result] = safeTry(() => JSON.parse('{"x":1}'));
|
|
21
25
|
|
|
22
26
|
if (err) {
|
|
23
|
-
console.error(err);
|
|
27
|
+
console.error(err);
|
|
24
28
|
} else {
|
|
25
29
|
console.log(result); // { x: 1 }
|
|
26
30
|
}
|
|
27
31
|
```
|
|
28
32
|
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
### Asynchronous example
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const { safeTryAsync } = require("safe-try-with-ai");
|
|
39
|
+
|
|
40
|
+
const [err, data] = await safeTryAsync(async () => {
|
|
41
|
+
return await fetchData();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (err) {
|
|
45
|
+
console.error(err);
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
29
51
|
## Optional AI Runtime Suggestions
|
|
30
52
|
|
|
31
53
|
Enable AI-style runtime suggestions by passing `{ analyze: true }` as the second argument.
|
|
32
54
|
|
|
33
|
-
|
|
55
|
+
```js
|
|
56
|
+
const { safeTry } = require("safe-try-with-ai");
|
|
57
|
+
|
|
58
|
+
const [err] = safeTry(() => JSON.parse("invalid"), { analyze: true });
|
|
59
|
+
|
|
60
|
+
if (err) {
|
|
61
|
+
console.error("Error:", err.message);
|
|
62
|
+
console.log("Suggestion:", err.suggestion);
|
|
63
|
+
console.log("Fix:", err.fix);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
> AI-style suggestions are **rule-based and local**.
|
|
68
|
+
> No real AI model, no network calls, no data collection.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Default Fallback Value
|
|
73
|
+
|
|
74
|
+
Use `safeTryDefault` to return a fallback value when an error occurs.
|
|
34
75
|
|
|
35
76
|
```js
|
|
36
|
-
const
|
|
77
|
+
const { safeTryDefault } = require("safe-try-with-ai");
|
|
78
|
+
|
|
79
|
+
const result = safeTryDefault(
|
|
80
|
+
() => JSON.parse("invalid"),
|
|
81
|
+
{},
|
|
82
|
+
{ analyze: true }
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
console.log(result); // {}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Safe JSON Parsing
|
|
91
|
+
|
|
92
|
+
```js
|
|
93
|
+
const { safeTryJson } = require("safe-try-with-ai");
|
|
94
|
+
|
|
95
|
+
const [err, data] = safeTryJson('{"x":1}', { analyze: true });
|
|
37
96
|
|
|
38
97
|
if (err) {
|
|
39
|
-
console.error(
|
|
40
|
-
console.log("Suggestion:", err.suggestion); // AI suggestion
|
|
41
|
-
console.log("Fix:", err.fix); // Suggested fix
|
|
98
|
+
console.error(err);
|
|
42
99
|
}
|
|
43
100
|
```
|
|
44
101
|
|
|
45
|
-
|
|
102
|
+
---
|
|
46
103
|
|
|
47
|
-
|
|
48
|
-
- Eliminates repetitive try/catch blocks
|
|
49
|
-
- Optional AI-style runtime error suggestions
|
|
50
|
-
- Zero dependencies
|
|
51
|
-
- Lightweight and fast
|
|
104
|
+
## CLI Usage
|
|
52
105
|
|
|
106
|
+
## CLI Usage
|
|
53
107
|
|
|
54
|
-
|
|
108
|
+
Validate JSON files from the terminal:
|
|
55
109
|
|
|
56
|
-
|
|
110
|
+
npx safe-try-with-ai example.json
|
|
111
|
+
npx safe-try-with-ai example.json --analyze
|
|
112
|
+
|
|
113
|
+
✔ = JSON valid (green)
|
|
114
|
+
✖ = JSON invalid (red)
|
|
115
|
+
Suggestions (blue)
|
|
116
|
+
Fix (green)
|
|
117
|
+
|
|
118
|
+
Exit codes:
|
|
119
|
+
0 = valid JSON
|
|
120
|
+
1 = invalid JSON or runtime error
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
npx safe-try-with-ai example.json
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Example output:
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
Error: Unexpected token
|
|
132
|
+
Suggestion: Check JSON formatting
|
|
133
|
+
Fix: Ensure commas and brackets are correct
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## TypeScript Support
|
|
139
|
+
|
|
140
|
+
Built-in TypeScript definitions included.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { safeTry } from "safe-try-with-ai";
|
|
57
144
|
|
|
145
|
+
const [err, result] = safeTry(() => JSON.parse(data));
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
No configuration required.
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## Features
|
|
153
|
+
|
|
154
|
+
* Works with synchronous and asynchronous functions
|
|
155
|
+
* Eliminates repetitive try/catch blocks
|
|
156
|
+
* Optional AI-style runtime suggestions
|
|
157
|
+
* Default fallback handling
|
|
158
|
+
* Safe JSON parsing helper
|
|
159
|
+
* CLI support via `npx`
|
|
160
|
+
* Built-in TypeScript definitions
|
|
161
|
+
* Zero dependencies
|
|
162
|
+
* Lightweight and fast
|
|
163
|
+
|
|
164
|
+
---
|
|
58
165
|
|
|
59
166
|
## Changelog
|
|
60
167
|
|
|
168
|
+
### v1.3.0
|
|
169
|
+
|
|
170
|
+
* Added TypeScript definitions
|
|
171
|
+
* Added CLI support
|
|
172
|
+
* Documentation improvements
|
|
173
|
+
|
|
61
174
|
### v1.2.0
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
175
|
+
|
|
176
|
+
* Added `safeTryDefault`
|
|
177
|
+
* Added `safeTryJson`
|
|
178
|
+
* Improved AI-style suggestions
|
|
66
179
|
|
|
67
180
|
### v1.1.1
|
|
68
|
-
|
|
181
|
+
|
|
182
|
+
* Improved JSON error suggestions
|
|
183
|
+
|
|
184
|
+
---
|
|
185
|
+
|
|
186
|
+
## License
|
|
187
|
+
|
|
188
|
+
MIT
|
|
69
189
|
|
|
70
190
|
---
|