safe-try-with-ai 1.0.0 → 1.1.1

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 +53 -3
  2. package/package.json +1 -1
  3. package/src/index.js +67 -17
package/README.md CHANGED
@@ -1,8 +1,58 @@
1
- # safe-try
1
+ # safe-try-with-ai
2
2
 
3
- A lightweight JavaScript utility for clean error handling without repetitive try/catch blocks.
3
+ A lightweight JavaScript utility for **clean error handling** with optional **AI-style runtime suggestions**, without repetitive try/catch blocks.
4
+
5
+ ---
4
6
 
5
7
  ## Installation
8
+
6
9
  ```bash
7
- npm install safe-try
10
+ npm install safe-try-with-ai
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Synchronous example
16
+
17
+ ```js
18
+ const { safeTry } = require("safe-try-with-ai");
19
+
20
+ const [err, result] = safeTry(() => JSON.parse('{"x":1}'));
21
+
22
+ if (err) {
23
+ console.error(err); // Original error
24
+ } else {
25
+ console.log(result); // { x: 1 }
26
+ }
27
+ ```
28
+
29
+ ## Optional AI Runtime Suggestions
30
+
31
+ Enable AI-style runtime suggestions by passing `{ analyze: true }` as the second argument.
32
+
33
+ ### Synchronous example
34
+
35
+ ```js
36
+ const [err, result] = safeTry(() => JSON.parse("invalid"), { analyze: true });
37
+
38
+ if (err) {
39
+ console.error("Error:", err.message); // Original error
40
+ console.log("Suggestion:", err.suggestion); // AI suggestion
41
+ console.log("Fix:", err.fix); // Suggested fix
42
+ }
43
+ ```
44
+
45
+ ## Features
46
+
47
+ - Works with synchronous and asynchronous functions
48
+ - Eliminates repetitive try/catch blocks
49
+ - Optional AI-style runtime error suggestions
50
+ - Zero dependencies
51
+ - Lightweight and fast
52
+
53
+
54
+ ## License
55
+
56
+ MIT
8
57
 
58
+ ---
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "safe-try-with-ai",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Clean error handling for JavaScript",
5
5
  "keywords": [
6
6
  "error-handling",
package/src/index.js CHANGED
@@ -1,3 +1,64 @@
1
+ // src/index.js
2
+
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
+ }
17
+
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
+
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
+ }
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
+
48
+ // Default fallback
49
+ return {
50
+ name: error.name,
51
+ message: error.message,
52
+ suggestion: "Check stack trace and input values",
53
+ };
54
+ }
55
+
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
+ */
1
62
  function safeTry(fn, options = {}) {
2
63
  try {
3
64
  const result = fn();
@@ -10,6 +71,12 @@ function safeTry(fn, options = {}) {
10
71
  }
11
72
  }
12
73
 
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
+ */
13
80
  async function safeTryAsync(fn, options = {}) {
14
81
  try {
15
82
  const result = await fn();
@@ -22,21 +89,4 @@ async function safeTryAsync(fn, options = {}) {
22
89
  }
23
90
  }
24
91
 
25
- function analyzeError(error) {
26
- if (/undefined/.test(error.message)) {
27
- return {
28
- name: error.name,
29
- message: error.message,
30
- suggestion: "Check if the variable exists before access",
31
- fix: "Use optional chaining or input validation"
32
- };
33
- }
34
-
35
- return {
36
- name: error.name,
37
- message: error.message,
38
- suggestion: "Check stack trace and input values"
39
- };
40
- }
41
-
42
92
  module.exports = { safeTry, safeTryAsync };