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

Result<T, E> (typescript generation) #281

Open
corbanbrook opened this issue Apr 15, 2024 · 1 comment
Open

Result<T, E> (typescript generation) #281

corbanbrook opened this issue Apr 15, 2024 · 1 comment

Comments

@corbanbrook
Copy link

In the latest version of webrpc there was added a selection of errors that extend from the WebRpcError class, as well as ways of defining Custom errors like UserNotFoundError. Which we can now use instanceof to handle different error cases easier. This is a great but im wondering if there is room for improvement by exposing which errors can be returned by a function so that our userland error handling code can exhuastively handle each case.

A current weakness of typescript is lack of a way to specify which errors may be thrown from a function. See discussion for possible function throws clause going back to 2016 and Ryans final word of the subject and rejection. Without typescript support for thrown errors we can however "lift" a thrown error and return it in a Result<T, E> type eg.

type Result<T, E = Error> =
  | { ok: true; value: T }
  | { ok: false; error: E };
   getUserByAddress(args: GetFriendByAddressArgs, headers?: object): Promise<<Result<GetUserByAddressReturn, WebRpcError | UserNotFoundError>>

Above the function is able to specify which errors it will potentially return:

Result<GetUserByAddressReturn, WebRpcError | UserNotFoundError>

References:

@VojtechVitek
Copy link
Contributor

Currently we don't have a way to distinguish what errors can be returned by individual RPC endpoints. We define custom errors in RIDL globally and not per RPC method.

I see how useful it could be to tell what errors you can get from individual RPC method. However, I don't see how we could implement this.

Would Result<GetUserByAddressReturn, WebRpcError> be any helpful?

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

No branches or pull requests

2 participants