thegecko/webusb

Use DOMException instead of Error

Opened this issue · 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');
    }
}

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

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

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