webusb icon indicating copy to clipboard operation
webusb copied to clipboard

Use DOMException instead of Error

Open dsanders11 opened this issue 6 years ago • 3 comments

For the errors listed in the spec (InvalidStateError, NotFoundError, etc), these should be DOMExceptions with the name set accordingly.

Example:

throw new DOMException('interface not found', 'NotFoundError');

To make it cleaner to implement in the code, may consider using a helper class for each error:

class NotFoundError {
    constructor(message) {
        return new DOMException(message, 'NotFoundError');
    }
}

dsanders11 avatar Dec 19 '18 20:12 dsanders11

Hmm, I believe DOMException is unique to browser environments. Not sure it makes sense to use it in other runtimes such as node.

thegecko avatar Dec 19 '18 23:12 thegecko

@thegecko, you're right. 🙂 My use-case for this library at the moment is in Electron, as they don't currently support native WebUSB due to issues with the browser UI needing to ask permission before accessing devices. As such I'd like my code to be as API-compatible as possible so I can (in theory) seamlessly switch between the two.

What about at least an error which emulates the shape of DOMException? Namely that it would have a name property? That seems to be the only real difference between a normal Error, and would allow code which checks the name of the error to work with both this library, and a browser implementation.

dsanders11 avatar Dec 20 '18 01:12 dsanders11

I'm happy with that approach, recommend we create an interface to match DOMException for this purpose. I'll think about a name.

thegecko avatar Dec 20 '18 14:12 thegecko