Skip to content

Commit 03aecd4

Browse files
committed
initial commit
0 parents  commit 03aecd4

File tree

9 files changed

+260
-0
lines changed

9 files changed

+260
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

decorator.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { isAsync } from './isAsync';
2+
3+
export default function erria(
4+
target: Object,
5+
propertyName: string,
6+
propertyDesciptor: PropertyDescriptor): any {
7+
8+
if (typeof target[propertyName] !== 'function') return;
9+
const method = propertyDesciptor.value;
10+
11+
if (isAsync(method)) {
12+
propertyDesciptor.value = async function (...args: any[]) {
13+
try {
14+
const result = await method.apply(this, args);
15+
return [result, null];
16+
} catch(err) {
17+
return [null, err]
18+
}
19+
}
20+
} else {
21+
propertyDesciptor.value = function (...args: any[]) {
22+
try {
23+
const result = method.apply(this, args);
24+
return [result, null];
25+
} catch(err) {
26+
return [null, err]
27+
}
28+
}
29+
}
30+
31+
return propertyDesciptor;
32+
};

index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { isAsync, isPromise } from './isAsync';
2+
3+
export default function erria(func : Function, ...args) : [any, Error] | Promise<[any, Error]> | any {
4+
let useResolve = false;
5+
try {
6+
const exec = func(...args);
7+
if (isPromise(exec)) {
8+
useResolve = true;
9+
return new Promise((resolve) => {
10+
exec
11+
.then(result => resolve([result, null]))
12+
.catch(err => resolve([null, err]))
13+
});
14+
} else {
15+
return [exec, null];
16+
}
17+
} catch(err) {
18+
if (useResolve) {
19+
return new Promise((resolve) => {
20+
resolve([null, err])
21+
});
22+
} else {
23+
return [null, err];
24+
}
25+
}
26+
};

isAsync.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// credit to stackoverflow
2+
3+
export function isAsync (func) {
4+
const string = func.toString().trim();
5+
6+
return !!(
7+
// native
8+
string.match(/^async /) ||
9+
// babel (this may change, but hey...)
10+
string.match(/return _ref[^\.]*\.apply/)
11+
// insert your other dirty transpiler check
12+
13+
// there are other more complex situations that maybe require you to check the return line for a *promise*
14+
);
15+
}
16+
17+
export function isPromise (obj) {
18+
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
19+
}

package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "erria",
3+
"version": "0.1.0",
4+
"description": "return error in array, remove the need of using try catch",
5+
"main": "index.ts",
6+
"scripts": {
7+
"test": "ts-node test.ts"
8+
},
9+
"keywords": [
10+
"Error",
11+
"Try",
12+
"catch",
13+
"typescript"
14+
],
15+
"author": "Le Quang Huy <[email protected]>",
16+
"license": "ISC",
17+
"devDependencies": {
18+
"ts-node": "^8.5.4",
19+
"typescript": "^3.7.3"
20+
}
21+
}

readme.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## Erria
2+
3+
**Erria** (Error in array) is inspired by the way Golang function can return mutiple value. Reduce the need of try catch code blocks.
4+
5+
## Install
6+
7+
```
8+
yarn add erria
9+
```
10+
or
11+
```
12+
npm i --save erria
13+
```
14+
15+
## Usage
16+
17+
This module is written in Typescript, pull requesta for regular js project are welcomed
18+
19+
### Regular synchronous and asynchronous function
20+
21+
```
22+
import erria from 'erria';
23+
24+
const start = async () => {
25+
const [res, err] = await erria(simpleAsync, someParameter, someMoreParameter);
26+
console.log([res, err]);
27+
28+
const [res2, err2] = erria(simpleSync);
29+
console.log([res2, err2]);
30+
};
31+
32+
start();
33+
```
34+
35+
### Decorator
36+
37+
```
38+
import erria from 'erria/decorator';
39+
40+
class Foo {
41+
42+
@erria
43+
async bar() : Promise<[any, Error] | any> {
44+
const res = await simpleAsync(someParameter);
45+
return res;
46+
}
47+
}
48+
49+
const start = async () => {
50+
const foo = new Foo();
51+
const [res3, err3] = await foo.bar();
52+
console.log([res3, err3]);
53+
};
54+
55+
start();
56+
```

test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import erria from './index';
2+
import erriaDeco from './decorator';
3+
4+
const simpleAsync = (something) => new Promise((resolve, reject) => {
5+
setTimeout(() => {
6+
console.log(something);
7+
reject(new Error("Fail intentionally"))
8+
}, 2000);
9+
});
10+
11+
const simpleSync = () => {
12+
const foo : any = {};
13+
return foo.bar.failIntentionally();
14+
};
15+
16+
class Foo {
17+
18+
@erriaDeco
19+
async bar() : Promise<[any, Error] | any> {
20+
const res = await simpleAsync(200);
21+
return res;
22+
}
23+
}
24+
25+
const start = async () => {
26+
const [res, err] = await erria(simpleAsync, 100);
27+
console.log([res, err]);
28+
29+
const [res2, err2] = erria(simpleSync);
30+
console.log([res2, err2]);
31+
32+
const foo = new Foo();
33+
const [res3, err3] = await foo.bar();
34+
console.log([res3, err3]);
35+
};
36+
37+
start();

tsconfig.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"skipLibCheck": true,
5+
"strict": false,
6+
"module": "commonjs",
7+
"target": "es2017",
8+
"sourceMap": true,
9+
"experimentalDecorators": true
10+
}
11+
}

yarn.lock

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
arg@^4.1.0:
6+
version "4.1.2"
7+
resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.2.tgz#e70c90579e02c63d80e3ad4e31d8bfdb8bd50064"
8+
integrity sha512-+ytCkGcBtHZ3V2r2Z06AncYO8jz46UEamcspGoU8lHcEbpn6J77QK0vdWvChsclg/tM5XIJC5tnjmPp7Eq6Obg==
9+
10+
buffer-from@^1.0.0:
11+
version "1.1.1"
12+
resolved "https://registry.yarnpkg.com/buffer-from/-/buffer-from-1.1.1.tgz#32713bc028f75c02fdb710d7c7bcec1f2c6070ef"
13+
integrity sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==
14+
15+
diff@^4.0.1:
16+
version "4.0.1"
17+
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.1.tgz#0c667cb467ebbb5cea7f14f135cc2dba7780a8ff"
18+
integrity sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==
19+
20+
make-error@^1.1.1:
21+
version "1.3.5"
22+
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.5.tgz#efe4e81f6db28cadd605c70f29c831b58ef776c8"
23+
integrity sha512-c3sIjNUow0+8swNwVpqoH4YCShKNFkMaw6oH1mNS2haDZQqkeZFlHS3dhoeEbKKmJB4vXpJucU6oH75aDYeE9g==
24+
25+
source-map-support@^0.5.6:
26+
version "0.5.16"
27+
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.5.16.tgz#0ae069e7fe3ba7538c64c98515e35339eac5a042"
28+
integrity sha512-efyLRJDr68D9hBBNIPWFjhpFzURh+KJykQwvMyW5UiZzYwoF6l4YMMDIJJEyFWxWCqfyxLzz6tSfUFR+kXXsVQ==
29+
dependencies:
30+
buffer-from "^1.0.0"
31+
source-map "^0.6.0"
32+
33+
source-map@^0.6.0:
34+
version "0.6.1"
35+
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263"
36+
integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==
37+
38+
ts-node@^8.5.4:
39+
version "8.5.4"
40+
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-8.5.4.tgz#a152add11fa19c221d0b48962c210cf467262ab2"
41+
integrity sha512-izbVCRV68EasEPQ8MSIGBNK9dc/4sYJJKYA+IarMQct1RtEot6Xp0bXuClsbUSnKpg50ho+aOAx8en5c+y4OFw==
42+
dependencies:
43+
arg "^4.1.0"
44+
diff "^4.0.1"
45+
make-error "^1.1.1"
46+
source-map-support "^0.5.6"
47+
yn "^3.0.0"
48+
49+
typescript@^3.7.3:
50+
version "3.7.3"
51+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.7.3.tgz#b36840668a16458a7025b9eabfad11b66ab85c69"
52+
integrity sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw==
53+
54+
yn@^3.0.0:
55+
version "3.1.1"
56+
resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50"
57+
integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==

0 commit comments

Comments
 (0)