Skip to content

web-sys: More precise exception types #2004

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

Closed
strax opened this issue Feb 17, 2020 · 1 comment
Closed

web-sys: More precise exception types #2004

strax opened this issue Feb 17, 2020 · 1 comment

Comments

@strax
Copy link

strax commented Feb 17, 2020

Motivation

In web-sys, operations that can fail currently return Result<T, JsValue>. The WebIDL specification lists the exceptions that specification-compliant implementations may throw, namely:

  • EvalError
  • RangeError
  • ReferenceError
  • TypeError
  • URIError
  • DOMException

Since all web_sys operations are derived from WebIDL, it seems that the error type could be narrowed to the above set.

Proposed Solution

Errors thrown by the WebIDL ECMAScript bindings could be converted to Rust structs implementing Error. This would further reduce the need of exposing JS objects to the Rust code; for example, parameters are already passed as Rust values and not JsValues, so modelling errors as Rust values would further hide the JS glue layer as an implementation detail. No idea about the potential performance hit though.

Has this behaviour been considered before and what are the motivations behind the current one?

@Pauan
Copy link
Contributor

Pauan commented Feb 17, 2020

The issue is that the WebIDL does not specify which errors are thrown by which methods. In fact, it doesn't even mention whether the method throws at all, which is why web-sys has to conservatively assume that all methods might throw.

In addition, the above list is not exhaustive, because other specifications can create new error types. As an example, the FileReader spec can throw NotFoundError, NotReadableError, and SecurityError. And IndexedDB can throw a whole slew of errors. And unfortunately this information isn't in the WebIDL either.

So we can't do this because the WebIDL just doesn't give enough information. However, gloo contains hand-crafted high-level APIs which can provide more meaningful error types (such as the gloo-file crate which defines a FileReadError enum).

@Pauan Pauan closed this as completed Feb 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants