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 25, 2023
1 parent 34e4bb0 commit f779ba6
Show file tree
Hide file tree
Showing 6 changed files with 77 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"
}
}
8 changes: 8 additions & 0 deletions application/api/console.1/content.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace api.console.content {
type Code = 'ENOTFOUND' | 'EPARSE';

class CustomError extends DomainError {
constructor(code?: Code);
toJSON(): object;
}
}
4 changes: 4 additions & 0 deletions application/api/console.1/content.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
({
access: 'public',

async method({ name }) {
// Try type: new api.console.content.CustomError('EPARSE');
const filePath = `/content/${name}.md`;
const buffer = application.resources.get(filePath);
if (!buffer) return new Error('Content is not found');
return { text: buffer.toString() };
},

CustomError: class CustomError extends DomainError {},
});
8 changes: 8 additions & 0 deletions application/api/example.1/add.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace api.example.add {
type Code = 'EARGA' | 'EARGB';

class CustomError extends DomainError {
constructor(code?: Code);
toJSON(): object;
}
}
31 changes: 31 additions & 0 deletions application/api/example.1/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,40 @@
},

method: async ({ a, b }) => {
new api.example.add.CustomError('EARGA');
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;
},

CustomError: class CustomError extends DomainError {
toJSON() {
const { name, code, message, stack } = this;
return { name, code, message, stack };
}
},
});
24 changes: 24 additions & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,27 @@ declare global {
const pg: Database;
}
}

export interface ErrorOptions {
code?: number | string;
cause?: Error;
}

export class Error extends global.Error {
constructor(message: string, options?: number | string | ErrorOptions);
message: string;
stack: string;
code?: number | string;
cause?: Error;
}

type Errors = Record<string, string>;

export class DomainError extends Error {
constructor(code?: string, options?: number | string | ErrorOptions);
message: string;
stack: string;
code?: number | string;
cause?: Error;
toError(errors: Errors): Error;
}

0 comments on commit f779ba6

Please sign in to comment.