Skip to content

Commit f779ba6

Browse files
committed
Error handling proposal
1 parent 34e4bb0 commit f779ba6

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace api.console.content {
2+
type Code = 'ENOTFOUND' | 'EPARSE';
3+
4+
class CustomError extends DomainError {
5+
constructor(code?: Code);
6+
toJSON(): object;
7+
}
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
({
22
access: 'public',
3+
34
async method({ name }) {
5+
// Try type: new api.console.content.CustomError('EPARSE');
46
const filePath = `/content/${name}.md`;
57
const buffer = application.resources.get(filePath);
68
if (!buffer) return new Error('Content is not found');
79
return { text: buffer.toString() };
810
},
11+
12+
CustomError: class CustomError extends DomainError {},
913
});

application/api/example.1/add.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace api.example.add {
2+
type Code = 'EARGA' | 'EARGB';
3+
4+
class CustomError extends DomainError {
5+
constructor(code?: Code);
6+
toJSON(): object;
7+
}
8+
}

application/api/example.1/add.js

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

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

1219
returns: 'number',
20+
21+
errors: {
22+
EARGA: 'Invalid argument: a',
23+
EARGB: 'Invalid argument: b',
24+
},
25+
26+
onError(error) {
27+
if (error.code in this.errors) {
28+
console.log(`Domain error detected: ${error.code}`);
29+
}
30+
return error;
31+
},
32+
33+
onException(error) {
34+
console.log(`Exception throws: ${error.message}`);
35+
return error;
36+
},
37+
38+
CustomError: class CustomError extends DomainError {
39+
toJSON() {
40+
const { name, code, message, stack } = this;
41+
return { name, code, message, stack };
42+
}
43+
},
1344
});

types/global.d.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,27 @@ declare global {
2323
const pg: Database;
2424
}
2525
}
26+
27+
export interface ErrorOptions {
28+
code?: number | string;
29+
cause?: Error;
30+
}
31+
32+
export class Error extends global.Error {
33+
constructor(message: string, options?: number | string | ErrorOptions);
34+
message: string;
35+
stack: string;
36+
code?: number | string;
37+
cause?: Error;
38+
}
39+
40+
type Errors = Record<string, string>;
41+
42+
export class DomainError extends Error {
43+
constructor(code?: string, options?: number | string | ErrorOptions);
44+
message: string;
45+
stack: string;
46+
code?: number | string;
47+
cause?: Error;
48+
toError(errors: Errors): Error;
49+
}

0 commit comments

Comments
 (0)