breautek commented on issue #693:
URL: 
https://github.com/apache/cordova-plugin-camera/issues/693#issuecomment-740630489


   > I assume, this could be a general point for discussion. My gut feeling 
says that a lot of plugins will use string messages over real error objects.
   
   While that might be true, it doesn't mean Apache Cordova shouldn't set a 
standard. I think it's a good idea to actually create instances of `Error` 
objects (or an extension of) when propagating errors.
   
   e.g:
   
   ```javascript
   class CameraError extends Error {
       constructor(code, message) {
           super("Code: " + code + ": " + message);
           this.code = code;
       }
   }
   
   var x = new CameraError(15, 'test error');
   x.message; // Code: 15: test error
   x.code; // 15
   ```
   
   will report to the console:
   
   ```
   Error: Code: 15: test error
       at <anonymous>:1:9
   ```
   
   I'm not _sure_ how safe it is to use `class` style syntax without the use of 
Babel or some other transpiler... we may have to rely on the prototype syntax 
which I think does affect the call stack by including the the error 
constructor, but I think that is minor.
   
   Extending `Error` has a couple of benefits, namely:
   - Ability to generically test for errors via `x instanceof Error`
   - We can also test for specific errors via `x instanceof CameraError`
   - We have the ability to attach additional details onto the error object 
such as the `code` property.
   - Ability to enforce consistency
   
   > export enum Error {
   
   Enums is also good, I'm surprised this plugin is actually missing them. 
Other plugins like the `cordova-plugin-file` has them (although, our projects 
are not in typescript so they are just simply objects, but serve the same 
purpose). I would advise against clobbering `Error` though since that is a 
browser object. 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to