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’ll 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

Conversation

tshemsedinov
Copy link
Member

No description provided.

@tshemsedinov tshemsedinov force-pushed the error-handling-proposal-timur branch from 5f903a5 to f779ba6 Compare June 25, 2023 02:33
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.

Comment on lines +21 to +27
errors: {
EARGA: 'Invalid argument: a',
EARGB: 'Invalid argument: b',
},

onError(error) {
if (error.code in this.errors) {
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.

cause?: Error;
}

export class Error extends global.Error {
Copy link
Member

@lundibundi lundibundi Jun 27, 2023

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

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?

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants