safe-wrapper 2.0.1 → 2.1.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 +34 -5
- package/lib/index.cjs +1 -1
- package/lib/index.d.ts +9 -5
- package/lib/index.mjs +1 -1
- package/package.json +1 -1
- /package/{2.0.0 → 2.0.1} +0 -0
package/README.md
CHANGED
|
@@ -11,7 +11,9 @@ safe-wrapper is a lightweight utility for javascript that simplifies error handl
|
|
|
11
11
|
|
|
12
12
|
- handles synchronous and asynchronous functions.
|
|
13
13
|
- supports specifying error types to control which errors are caught and handled.
|
|
14
|
-
- returns consistent
|
|
14
|
+
- returns a consistent response in `[error, result]` format,
|
|
15
|
+
- `error`: null if successful, else an error (or transformed object).
|
|
16
|
+
- `result`: return value (sync) or resolved value (async) when successful, else null.
|
|
15
17
|
- supports custom error transformation for advanced error handling.
|
|
16
18
|
|
|
17
19
|
### Installation
|
|
@@ -92,7 +94,7 @@ const safeSync = safe(sync, [TypeError, RangeError]);
|
|
|
92
94
|
const [error, result] = safeSync(args);
|
|
93
95
|
```
|
|
94
96
|
|
|
95
|
-
#### using custom error transformer
|
|
97
|
+
#### using synchronous custom error transformer
|
|
96
98
|
|
|
97
99
|
you can provide a custom error transformer function to modify how errors are handled:
|
|
98
100
|
|
|
@@ -106,13 +108,40 @@ const transformer = (error) => ({
|
|
|
106
108
|
});
|
|
107
109
|
|
|
108
110
|
const safeWithTransform = safe(
|
|
109
|
-
() => { throw new Error('custom error'); },
|
|
111
|
+
() => { throw new Error('custom sync error'); },
|
|
110
112
|
[Error],
|
|
111
113
|
transformer
|
|
112
114
|
);
|
|
113
115
|
|
|
114
116
|
const [error, result] = safeWithTransform();
|
|
115
|
-
// error will be: { code: 'Error', message: 'custom error', timestamp: Date }
|
|
117
|
+
// error will be: { code: 'Error', message: 'custom sync error', timestamp: Date }
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### using asynchronous custom error transformer
|
|
121
|
+
|
|
122
|
+
you can provide an asynchronous custom error transformer to modify how errors are handled.
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
import { safe } from 'safe-wrapper';
|
|
126
|
+
|
|
127
|
+
const transformer = async (error) => {
|
|
128
|
+
await report(error);
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
code: error.name,
|
|
132
|
+
message: error.message,
|
|
133
|
+
timestamp: new Date()
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const safeWithTransform = safe(
|
|
138
|
+
async () => { throw new Error('custom async error'); },
|
|
139
|
+
[Error],
|
|
140
|
+
transformer
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
const [error, result] = await safeWithTransform();
|
|
144
|
+
// error will be: { code: 'Error', message: 'custom async error', timestamp: Date }
|
|
116
145
|
```
|
|
117
146
|
|
|
118
147
|
#### wrapping built-in functions
|
|
@@ -138,4 +167,4 @@ const [error, result] = safe(Object.keys, [TypeError])(null);
|
|
|
138
167
|
- transformer (function, optional): a function that takes an error object and returns a transformed version of it. if not provided, the original error is used.
|
|
139
168
|
- returns `[error, result]`
|
|
140
169
|
- error (error | null): the error object if error occurred, otherwise null. if an transformer is provided, this will be the transformed error.
|
|
141
|
-
- result (
|
|
170
|
+
- result (result | null): the result of the action function if no error occurred, otherwise null.
|
package/lib/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var f=Object.defineProperty;var d=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var h=Object.prototype.hasOwnProperty;var l=(n,e)=>{for(var t in e)f(n,t,{get:e[t],enumerable:!0})},m=(n,e,t,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let c of a(e))!h.call(n,c)&&c!==t&&f(n,c,{get:()=>e[c],enumerable:!(o=d(e,c))||o.enumerable});return n};var p=n=>m(f({},"__esModule",{value:!0}),n);var y={};l(y,{safe:()=>g});module.exports=p(y);var r=({error:n=null,data:e=null,result:t=void 0})=>[n,t!==void 0?t:e],s=n=>n instanceof Promise||typeof n?.then=="function",b=n=>(...e)=>{try{let t=n(...e);return s(t)?t.then(o=>r({error:o})).catch(o=>r({error:o})):r({error:t})}catch(t){return r({error:t})}},i=(n,e=[],t=void 0)=>{let o=!e.length||e.some(u=>n instanceof u),c=t!==void 0&&typeof t=="function";if(o)return c?b(t)(n):r({error:n});throw n},g=(n,e=[],t=void 0)=>(...o)=>{try{let c=n(...o);return s(c)?c.then(u=>r({data:u})).catch(u=>i(u,e,t)):r({result:c})}catch(c){return i(c,e,t)}};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* safely executes a synchronous or asynchronous action, handles any errors that occur and returns the result in a consistent response structure.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
* @
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
4
|
+
* @template R,
|
|
5
|
+
* @template [T = Error]
|
|
6
|
+
*
|
|
7
|
+
* @param {(...args: any[]) => R} action - the function to be wrapped.
|
|
8
|
+
* @param {Array<ErrorConstructor>} [types = []] - an optional array of error types to catch.
|
|
9
|
+
* @param {(error: Error) => Promise<T> | T} [transformer] - an optional function to transform the error object.
|
|
10
|
+
*
|
|
11
|
+
* @returns {(...args: Parameters<typeof action>) => R extends Promise<infer U> ? Promise<[T, null] | [null, U]> : [T, null] | [null, R]} - a tuple where the first element is the error object, if available, or the transformed error object, if a transformer function is provided. the second element is the result of the action, if available.
|
|
8
12
|
*/
|
|
9
|
-
export function safe(action:
|
|
13
|
+
export function safe<R, T = Error>(action: (...args: any[]) => R, types?: Array<ErrorConstructor>, transformer?: (error: Error) => Promise<T> | T): (...args: Parameters<typeof action>) => R extends Promise<infer U> ? Promise<[T, null] | [null, U]> : [T, null] | [null, R];
|
package/lib/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var r=({error:t=null,data:e=null,result:n=void 0})=>[t,n!==void 0?n:e],i=t=>t instanceof Promise||typeof t?.then=="function",s=t=>(...e)=>{try{let n=t(...e);return i(n)?n.then(c=>r({error:c})).catch(c=>r({error:c})):r({error:n})}catch(n){return r({error:n})}},f=(t,e=[],n=void 0)=>{let c=!e.length||e.some(u=>t instanceof u),o=n!==void 0&&typeof n=="function";if(c)return o?s(n)(t):r({error:t});throw t},d=(t,e=[],n=void 0)=>(...c)=>{try{let o=t(...c);return i(o)?o.then(u=>r({data:u})).catch(u=>f(u,e,n)):r({result:o})}catch(o){return f(o,e,n)}};export{d as safe};
|
package/package.json
CHANGED
/package/{2.0.0 → 2.0.1}
RENAMED
|
File without changes
|