safe-try-with-ai 1.0.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/LICENSE ADDED
@@ -0,0 +1 @@
1
+ Copyright (c) 2026 madhuka2002
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # safe-try
2
+
3
+ A lightweight JavaScript utility for clean error handling without repetitive try/catch blocks.
4
+
5
+ ## Installation
6
+ ```bash
7
+ npm install safe-try
8
+
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "safe-try-with-ai",
3
+ "version": "1.0.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 ADDED
@@ -0,0 +1,42 @@
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
+ }
12
+
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
+ }
24
+
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
+ module.exports = { safeTry, safeTryAsync };