apache / apache/cordova-plugin-camera
Throw structured errors instead of just messages
- Dominant language
- Objective-C
- Stars
- 976
- Forks
- 1.6k
- PR merge metrics
- No merged PRs in 30d
Description
# Feature Request
## Motivation Behind Feature
If the plugin threw structured errors having an error code and an error message, developers could handle these errors without relying on magic strings representing the error message.
For instance, if the camera permission was not granted on Android devices, the error is currently only '20'. If it was `{ code: '20', message: 'Camera permission not granted' }` instead, it would be more understandable and the error handler could differentiate based on the `code`.
## Feature Description
I suggest passing a structured error object to the `onError` handle as described above. Furthermore, it would be great to export an enum with all the error codes like
```
export enum Error {
NO_PERMISSION: '20',
USER_CANCELED: '99',
// ...
}
```
This would allow for handling errors like
```
navigator.camera.getPicture(
() => { /* handle success */ },
error => {
switch(error.code) {
case Error.NO_PERMISSION:
// show a popup asking to grant the permission
case Error.USER_CANCELED:
// do nothing as the user chose to cancel the action
...
}
},
cameraOptions,
);
```
This would break existing error handlers comparing with the message.
## Alternatives or Workarounds
Currently, error handling is only possible via comparing the error string with magic strings (i.e. the actual messages).
Contributor guide
Assessment
This issue has not been assessed yet.