Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unified errors and error codes #3673

Merged
merged 14 commits into from
Oct 19, 2020
Merged

feat: unified errors and error codes #3673

merged 14 commits into from
Oct 19, 2020

Conversation

imhoffd
Copy link
Contributor

@imhoffd imhoffd commented Oct 13, 2020

This unifies the base class of Capacitor exceptions to a new Capacitor.Exception class, allowing the following:

err instanceof Capacitor.Exception; // true

Additionally, the .unimplemented()/.unavailable() helpers on iOS, Android, and Web can now accept a message argument and use the new exception codes to allow the following:

e.code === ExceptionCode.Unimplemented; // true
e.code === ExceptionCode.Unavailable; // false

These codes make it easier for people to check what went wrong with Plugin API calls.

Because of the new availability of UNAVAILABLE on iOS and Web, it makes sense to remove UNSUPPORTED_BROWSER and use unavailable instead. Usage example:

--- a/haptics/src/web.ts
+++ b/haptics/src/web.ts
@@ -1,4 +1,4 @@
-import { UnsupportedBrowserException, WebPlugin } from '@capacitor/core';
+import { WebPlugin } from '@capacitor/core';

 import type {
   HapticsPlugin,
@@ -70,9 +70,7 @@ export class HapticsWeb extends WebPlugin implements HapticsPlugin {
     if (navigator.vibrate) {
       navigator.vibrate(pattern);
     } else {
-      throw new UnsupportedBrowserException(
-        'Browser does not support the vibrate API',
-      );
+      throw this.unavailable('Browser does not support the vibrate API');
     }
   }
 }

closes #3670

@imhoffd imhoffd requested a review from jcesarmobile October 13, 2020 20:29
@imhoffd imhoffd changed the title feat: UNIMPLEMENTED/UNAVAILABLE error codes feat: unimplemented/unavailable error codes Oct 13, 2020
@imhoffd imhoffd added this to the 3.0.0 milestone Oct 13, 2020
@imhoffd imhoffd changed the title feat: unimplemented/unavailable error codes feat: unified errors and error codes Oct 16, 2020
Copy link
Member

@jcesarmobile jcesarmobile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just two minor comments, other than that looks and works good.

@imhoffd imhoffd requested a review from jcesarmobile October 19, 2020 17:31
Copy link
Member

@jcesarmobile jcesarmobile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@imhoffd imhoffd merged commit f9e0803 into main Oct 19, 2020
@imhoffd imhoffd deleted the except branch October 19, 2020 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Investigate errors and error codes (UNAVAILABLE, UNIMPLEMENTED)
2 participants