Skip to content

Commit 5f903a5

Browse files
committed
Error handling proposal
1 parent 34e4bb0 commit 5f903a5

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

application/api/.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"db": "readonly",
1111
"bus": "readonly",
1212
"domain": "readonly",
13-
"metarhia": "readonly"
13+
"metarhia": "readonly",
14+
"DomainError": "readonly"
1415
}
1516
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type Code = 'ENOTFOUND' | 'EPARSE';
2+
3+
class DomainError {
4+
constructor(code: Code);
5+
}

application/api/console.1/content.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
({
22
access: 'public',
33
async method({ name }) {
4+
// it doesn't work in IDE, but works in runtime:
5+
// new DomainError('EPARSE');
46
const filePath = `/content/${name}.md`;
57
const buffer = application.resources.get(filePath);
68
if (!buffer) return new Error('Content is not found');

application/api/example.1/add.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,32 @@
55
},
66

77
method: async ({ a, b }) => {
8+
if (typeof a !== 'number') return new DomainError('EARGA');
9+
if (typeof b !== 'number') {
10+
return new api.example.example.CustomError('EARGB');
11+
}
12+
if (Number.isNaN(a)) throw Error('Not a number: a');
13+
if (Number.isNaN(b)) throw Error('Not a number: b');
814
const result = a + b;
915
return result;
1016
},
1117

1218
returns: 'number',
19+
20+
errors: {
21+
EARGA: 'Invalid argument: a',
22+
EARGB: 'Invalid argument: b',
23+
},
24+
25+
onError(error) {
26+
if (error.code in this.errors) {
27+
console.log(`Domain error detected: ${error.code}`);
28+
}
29+
return error;
30+
},
31+
32+
onException(error) {
33+
console.log(`Exception throws: ${error.message}`);
34+
return error;
35+
},
1336
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type Code = 'EARGA' | 'EARGB';
2+
3+
class DomainError {
4+
constructor(code: Code);
5+
}
6+
7+
namespace api.example.example {
8+
class CustomError extends DomainError {
9+
toJSON() {
10+
const { name, code, message, stack } = this;
11+
return { name, code, message, stack };
12+
}
13+
}
14+
}
15+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
({
2+
CustomError: class CustomError extends DomainError {
3+
toJSON() {
4+
const { name, code, message, stack } = this;
5+
return { name, code, message, stack };
6+
}
7+
},
8+
});

0 commit comments

Comments
 (0)