apache / apache/cordova-plugin-camera

Throw structured errors instead of just messages

Open
#693 4 comments 3 reactions 0 assignees View on GitHub
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.