safe-try-with-ai 1.1.0 → 1.2.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.
Files changed (3) hide show
  1. package/README.md +22 -1
  2. package/package.json +17 -17
  3. package/src/index.js +83 -80
package/README.md CHANGED
@@ -8,7 +8,7 @@ A lightweight JavaScript utility for **clean error handling** with optional **AI
8
8
 
9
9
  ```bash
10
10
  npm install safe-try-with-ai
11
-
11
+ ```
12
12
 
13
13
  ## Usage
14
14
 
@@ -24,6 +24,7 @@ if (err) {
24
24
  } else {
25
25
  console.log(result); // { x: 1 }
26
26
  }
27
+ ```
27
28
 
28
29
  ## Optional AI Runtime Suggestions
29
30
 
@@ -39,6 +40,7 @@ if (err) {
39
40
  console.log("Suggestion:", err.suggestion); // AI suggestion
40
41
  console.log("Fix:", err.fix); // Suggested fix
41
42
  }
43
+ ```
42
44
 
43
45
  ## Features
44
46
 
@@ -47,3 +49,22 @@ if (err) {
47
49
  - Optional AI-style runtime error suggestions
48
50
  - Zero dependencies
49
51
  - Lightweight and fast
52
+
53
+
54
+ ## License
55
+
56
+ MIT
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
+
70
+ ---
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
- "name": "safe-try-with-ai",
3
- "version": "1.1.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
- "scripts": {
16
- "test": "echo \"Error: no test specified\" && exit 1"
17
- }
18
- }
2
+ "name": "safe-try-with-ai",
3
+ "version": "1.2.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
+ "scripts": {
16
+ "test": "echo \"Error: no test specified\" && exit 1"
17
+ }
18
+ }
package/src/index.js CHANGED
@@ -1,92 +1,95 @@
1
- // src/index.js
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
- * Rule-based AI-style error analyzer
5
- * Returns enriched error object with suggestion and optional fix
6
- */
7
- function analyzeError(error) {
8
- // Handle undefined variable
9
- if (/undefined/.test(error.message)) {
10
- return {
11
- name: error.name,
12
- message: error.message,
13
- suggestion: "Check if the variable exists before accessing it",
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
- // Handle JSON parsing errors
29
- if (/JSON/.test(error.message)) {
30
- return {
31
- name: error.name,
32
- message: error.message,
33
- suggestion: "Check your JSON syntax",
34
- fix: "Use JSON validators or wrap JSON.parse in safeTry"
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
- // Default fallback
49
- return {
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
- * Safe synchronous try wrapper
58
- * @param {Function} fn - Function to execute
59
- * @param {Object} options - { analyze: boolean } optional runtime suggestions
60
- * @returns {[Error|null, any]} tuple of error and result
61
- */
62
- function safeTry(fn, options = {}) {
63
- try {
64
- const result = fn();
65
- return [null, result];
66
- } catch (error) {
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
- * Safe asynchronous try wrapper
76
- * @param {Function} fn - Async function to execute
77
- * @param {Object} options - { analyze: boolean } optional runtime suggestions
78
- * @returns {Promise<[Error|null, any]>} tuple of error and result
79
- */
80
- async function safeTryAsync(fn, options = {}) {
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
- return [error, null];
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 = { safeTry, safeTryAsync };
90
+ module.exports = {
91
+ safeTry,
92
+ safeTryAsync,
93
+ safeTryDefault,
94
+ safeTryJson
95
+ };