-
-
Notifications
You must be signed in to change notification settings - Fork 17
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 propagation #16
Comments
You are overthinking... Error propagation: const result = someMethod(); |
Sure. The syntax is the least important thing about it right now as long as it's clearly defined how it's going to work. Though I'd much rather it be explicit with |
@arthurfiorette As instance we need to be able to express something like that: interface IUserService {
register(userData: UserData): Promise<Result<User, UserRegistrationError>>;
} where export type UserRegistrationError =
| "ERR_FORBIDDEN_EMAIL_DOMAIN"
| "ERR_USER_IS_ALREADY_REGISTERED"
| "ERR_PASSWORD_TOO_WEAK"
| "ERR_PASSWORD_TOO_SHORT"
| "ERR_PASSWORD_TOO_LONG" In the same time we understand that Why do we need that? We are using GraphQL, so we generate the TS-typing for the GQL interface (aka mutations, types, etc), We are not unique, so there are a lot solutions like The best what we currently can have looks like the following: class UserService {
// -- snip --
register(userData: UserData) {
return Do(async function* () {
yield* await this.dataPolicy.validateEmail(userData.email);
yield* await this.dataPolicy.validatePassword(userData.password);
const passwordHash = await this.cryptoService.generatePasswordHash(userData.password);
const user = yield* await this.users.create({ ...userData, passwordHash });
return user;
});
}
} where interface IDataPolicy {
validateEmail(email: string): Promise<Result<void, "ERR_FORBIDDEN_EMAIL_DOMAIN">>;
validatePassword(password: string): Promise<Result<void, "ERR_PASSWORD_TOO_WEAK" | "ERR_PASSWORD_TOO_SHORT" | "ERR_PASSWORD_TOO_LONG">>
} interface IUserRepo {
create(userData: CreateUserData): Promise<Result<User, "ERR_USER_IS_ALREADY_REGISTERED">>
} So,
The In the same moment the possible exceptions are absolutely ignored here -- they will be There are cases when we need to transform the From the JavaScript point of view all of that doesn't make any sense: the errors couldn't be typed in any case. Current proposal improves error handling based on the untyped errors and seems to work for exceptions, We need more then just untyped error handling |
That's not what this proposal is trying to acheive neither wants to do so. |
yes, exactly, and that is a main problem with the proposal. TypeScripts also needs the I've written a little bit more here: microsoft/TypeScript#56365 (comment) |
I think the syntax for error propagation should be a part of this proposal. It would be nice to be able to leave the error handling to the caller (unless top level ofc) and just grab the result if it's successful.
Regular call:
With error propagation:
The text was updated successfully, but these errors were encountered: