Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling proposal #233

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rarely you need an error per-method.
It is much easier to manage errors defined in a separate file and exported everywhere.
This way format/code/message/interface is much easier to control.

});
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) {
Comment on lines +21 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is unlikely that you will know all errors that are thrown from inside the domain. Or more accurately - unlikely that you want to list them in every method. It adds a lot of chore to the api definition.
IMO it is better to manage this per-error and let domain decide which to throw.

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;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I"d suggest to have toJSON() and toUserError() defined as global interfaces as wel.

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

export class Error extends global.Error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to have some content object to each error. Only cause may not be enough.
And people will be forced to Object.assign(new Error(), { my custom fields })

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this method?

}