thegecko / webusb

Node.js implementation of the WebUSB Specification
https://thegecko.github.io/webusb/
MIT License
183 stars 27 forks source link

Use DOMException instead of Error #38

Open dsanders11 opened 5 years ago

dsanders11 commented 5 years ago

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');
    }
}
thegecko commented 5 years ago

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

dsanders11 commented 5 years ago

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

thegecko commented 5 years ago

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