Skip to content

Commit

Permalink
Error handling proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
tshemsedinov committed Jun 24, 2023
1 parent 34e4bb0 commit 5f903a5
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion application/api/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"db": "readonly",
"bus": "readonly",
"domain": "readonly",
"metarhia": "readonly"
"metarhia": "readonly",
"DomainError": "readonly"
}
}
5 changes: 5 additions & 0 deletions application/api/console.1/console.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Code = 'ENOTFOUND' | 'EPARSE';

class DomainError {
constructor(code: Code);
}
2 changes: 2 additions & 0 deletions application/api/console.1/content.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
({
access: 'public',
async method({ name }) {
// it doesn't work in IDE, but works in runtime:
// new DomainError('EPARSE');
const filePath = `/content/${name}.md`;
const buffer = application.resources.get(filePath);
if (!buffer) return new Error('Content is not found');
Expand Down
23 changes: 23 additions & 0 deletions application/api/example.1/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,32 @@
},

method: async ({ a, b }) => {
if (typeof a !== 'number') return new DomainError('EARGA');
if (typeof b !== 'number') {
return new api.example.example.CustomError('EARGB');
}
if (Number.isNaN(a)) throw Error('Not a number: a');
if (Number.isNaN(b)) throw Error('Not a number: b');
const result = a + b;
return result;
},

returns: 'number',

errors: {
EARGA: 'Invalid argument: a',
EARGB: 'Invalid argument: b',
},

onError(error) {
if (error.code in this.errors) {
console.log(`Domain error detected: ${error.code}`);
}
return error;
},

onException(error) {
console.log(`Exception throws: ${error.message}`);
return error;
},
});
15 changes: 15 additions & 0 deletions application/api/example.1/example.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Code = 'EARGA' | 'EARGB';

class DomainError {
constructor(code: Code);
}

namespace api.example.example {
class CustomError extends DomainError {
toJSON() {
const { name, code, message, stack } = this;
return { name, code, message, stack };
}
}
}

8 changes: 8 additions & 0 deletions application/api/example.1/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
({
CustomError: class CustomError extends DomainError {
toJSON() {
const { name, code, message, stack } = this;
return { name, code, message, stack };
}
},
});

0 comments on commit 5f903a5

Please sign in to comment.