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 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
- "name": "safe-try-with-ai",
3
- "version": "1.1.1",
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.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
- // 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
+ };