safe-try-with-ai 1.1.1 → 1.3.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/README.md +12 -0
- package/index.d.ts +39 -0
- package/package.json +18 -17
- package/src/index.js +83 -80
package/README.md
CHANGED
|
@@ -55,4 +55,16 @@ if (err) {
|
|
|
55
55
|
|
|
56
56
|
MIT
|
|
57
57
|
|
|
58
|
+
|
|
59
|
+
## Changelog
|
|
60
|
+
|
|
61
|
+
### v1.2.0
|
|
62
|
+
- Added `safeTryDefault` for fallback values
|
|
63
|
+
- Added `safeTryJson` for safe JSON parsing
|
|
64
|
+
- Improved AI-style runtime suggestions
|
|
65
|
+
- No breaking changes
|
|
66
|
+
|
|
67
|
+
### v1.1.1
|
|
68
|
+
- Improved JSON error suggestions
|
|
69
|
+
|
|
58
70
|
---
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
export interface AISuggestion {
|
|
2
|
+
name: string;
|
|
3
|
+
message: string;
|
|
4
|
+
suggestion?: string;
|
|
5
|
+
fix?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface SafeTryOptions {
|
|
9
|
+
analyze?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type SafeTryResult<T> = [null, T] | [Error | AISuggestion, null];
|
|
13
|
+
|
|
14
|
+
export function safeTry<T>(
|
|
15
|
+
fn: () => T,
|
|
16
|
+
options?: SafeTryOptions
|
|
17
|
+
): SafeTryResult<T>;
|
|
18
|
+
|
|
19
|
+
export function safeTryAsync<T>(
|
|
20
|
+
fn: () => Promise<T>,
|
|
21
|
+
options?: SafeTryOptions
|
|
22
|
+
): Promise<SafeTryResult<T>>;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Safely parse JSON with AI-style error suggestions
|
|
26
|
+
*/
|
|
27
|
+
export function safeTryJson<T = unknown>(
|
|
28
|
+
json: string,
|
|
29
|
+
options?: SafeTryOptions
|
|
30
|
+
): SafeTryResult<T>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Execute with a default fallback value on error
|
|
34
|
+
*/
|
|
35
|
+
export function safeTryDefault<T>(
|
|
36
|
+
fn: () => T,
|
|
37
|
+
defaultValue: T,
|
|
38
|
+
options?: SafeTryOptions
|
|
39
|
+
): T;
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
-
}
|
|
2
|
+
"name": "safe-try-with-ai",
|
|
3
|
+
"version": "1.3.0",
|
|
4
|
+
"description": "Clean error handling for JavaScript",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"error-handling",
|
|
7
|
+
"utility",
|
|
8
|
+
"functional",
|
|
9
|
+
"AI"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "madhuka2002",
|
|
13
|
+
"type": "commonjs",
|
|
14
|
+
"main": "src/index.js",
|
|
15
|
+
"types": "index.d.ts",
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,92 +1,95 @@
|
|
|
1
|
-
|
|
1
|
+
function safeTry(fn, options = {}) {
|
|
2
|
+
try {
|
|
3
|
+
const result = fn();
|
|
4
|
+
return [null, result];
|
|
5
|
+
} catch (error) {
|
|
6
|
+
if (options.analyze) {
|
|
7
|
+
return [analyzeError(error), null];
|
|
8
|
+
}
|
|
9
|
+
return [error, null];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
fix: "Use optional chaining (?.) or input validation"
|
|
15
|
-
};
|
|
16
|
-
}
|
|
13
|
+
async function safeTryAsync(fn, options = {}) {
|
|
14
|
+
try {
|
|
15
|
+
const result = await fn();
|
|
16
|
+
return [null, result];
|
|
17
|
+
} catch (error) {
|
|
18
|
+
if (options.analyze) {
|
|
19
|
+
return [analyzeError(error), null];
|
|
20
|
+
}
|
|
21
|
+
return [error, null];
|
|
22
|
+
}
|
|
23
|
+
}
|
|
17
24
|
|
|
18
|
-
// Handle property access errors
|
|
19
|
-
if (/Cannot read property/.test(error.message)) {
|
|
20
|
-
return {
|
|
21
|
-
name: error.name,
|
|
22
|
-
message: error.message,
|
|
23
|
-
suggestion: "Verify the object exists before accessing its property",
|
|
24
|
-
fix: "Use optional chaining (?.) or null checks"
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
25
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
function safeTryDefault(fn, defaultValue, options = {}) {
|
|
27
|
+
try {
|
|
28
|
+
const result = fn();
|
|
29
|
+
return [null, result];
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (options.analyze) {
|
|
32
|
+
return [analyzeError(error), defaultValue];
|
|
33
|
+
}
|
|
34
|
+
return [error, defaultValue];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
37
|
|
|
38
|
-
// Handle fetch/network errors
|
|
39
|
-
if (/fetch/.test(error.message)) {
|
|
40
|
-
return {
|
|
41
|
-
name: error.name,
|
|
42
|
-
message: error.message,
|
|
43
|
-
suggestion: "Check network connection and URL validity",
|
|
44
|
-
fix: "Ensure network is online and URL is correct"
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
38
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
name: error.name,
|
|
51
|
-
message: error.message,
|
|
52
|
-
suggestion: "Check stack trace and input values",
|
|
53
|
-
};
|
|
39
|
+
function safeTryJson(jsonString, options = {}) {
|
|
40
|
+
return safeTry(() => JSON.parse(jsonString), options);
|
|
54
41
|
}
|
|
55
42
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (options.analyze) {
|
|
68
|
-
return [analyzeError(error), null];
|
|
43
|
+
|
|
44
|
+
function analyzeError(error) {
|
|
45
|
+
const message = error.message || "";
|
|
46
|
+
|
|
47
|
+
if (/undefined/.test(message)) {
|
|
48
|
+
return {
|
|
49
|
+
name: error.name,
|
|
50
|
+
message,
|
|
51
|
+
suggestion: "Check if the variable exists before accessing it",
|
|
52
|
+
fix: "Use optional chaining or input validation"
|
|
53
|
+
};
|
|
69
54
|
}
|
|
70
|
-
return [error, null];
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
55
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
try {
|
|
82
|
-
const result = await fn();
|
|
83
|
-
return [null, result];
|
|
84
|
-
} catch (error) {
|
|
85
|
-
if (options.analyze) {
|
|
86
|
-
return [analyzeError(error), null];
|
|
56
|
+
if (/Unexpected token|Expected.*position/.test(message)) {
|
|
57
|
+
return {
|
|
58
|
+
name: error.name,
|
|
59
|
+
message,
|
|
60
|
+
suggestion: "Your JSON appears to be invalid",
|
|
61
|
+
fix: "Check for missing commas, quotes, or brackets"
|
|
62
|
+
};
|
|
87
63
|
}
|
|
88
|
-
|
|
89
|
-
|
|
64
|
+
|
|
65
|
+
if (error instanceof TypeError) {
|
|
66
|
+
return {
|
|
67
|
+
name: error.name,
|
|
68
|
+
message,
|
|
69
|
+
suggestion: "A value is being used with the wrong type",
|
|
70
|
+
fix: "Verify variable types before using them"
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (error instanceof ReferenceError) {
|
|
75
|
+
return {
|
|
76
|
+
name: error.name,
|
|
77
|
+
message,
|
|
78
|
+
suggestion: "A variable is not defined",
|
|
79
|
+
fix: "Declare the variable before using it"
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
name: error.name,
|
|
85
|
+
message,
|
|
86
|
+
suggestion: "Check stack trace and input values"
|
|
87
|
+
};
|
|
90
88
|
}
|
|
91
89
|
|
|
92
|
-
module.exports = {
|
|
90
|
+
module.exports = {
|
|
91
|
+
safeTry,
|
|
92
|
+
safeTryAsync,
|
|
93
|
+
safeTryDefault,
|
|
94
|
+
safeTryJson
|
|
95
|
+
};
|