Skip to content

fix(core): handle uncaught native keyboard exceptions - #27514

Merged
sean-perkins merged 4 commits into
mainfrom
sp/keyboard-resize-mode
May 24, 2023
Merged

sean-perkins merged 4 commits into
mainfrom
sp/keyboard-resize-mode

Conversation

@sean-perkins

@sean-perkins sean-perkins commented May 19, 2023 •

Copy link
Copy Markdown
Contributor

Issue number: Resolves #27503


What is the current behavior?

Ionic Framework wraps the implementation around Capacitor's Keyboard plugin API, to provide additional functionality and behavior "automatically" in Ionic, when the plugin is installed.

Certain methods such as getResizeMode() are only available for certain platforms and will cause an error when running in the unsupported platform.

Ionic Framework does not check to see if that platform is active before calling potentially unsupported methods, which leads to an exception for scenarios such as this - calling getResizeMode() on Android.

What is the new behavior?

  • Handles the uncaught exception by returning undefined if the plugin method is unavailable.
  • Developers do not receive an uncaught exception on Android when using the Capacitor Keyboard plugin with Ionic Framework

Does this introduce a breaking change?

  • Yes
  • No

Other information

Dev-build: 7.0.8-dev.11684444351.1b1ab142 (outdated)

@bolt-new-by-stackblitz

Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@github-actions github-actions Bot added the package: core @ionic/core package label May 19, 2023
@sean-perkins
sean-perkins marked this pull request as ready for review May 19, 2023 14:41
@sean-perkins
sean-perkins requested a review from a team as a code owner May 19, 2023 14:41

@thetaPC thetaPC left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@sean-perkins
sean-perkins requested a review from liamdebeasi May 22, 2023 18:21
@sean-perkins

Copy link
Copy Markdown
Contributor Author

@liamdebeasi I'm going to add you as an additional reviewer since you wrote a lot of the code around this. Any concerns to this approach?

Comment thread core/src/utils/native/keyboard.ts Outdated
getResizeMode(): Promise<KeyboardResizeOptions | undefined> {
const engine = this.getEngine();
if (!engine || !engine.getResizeMode) {
if (!isPlatform('ios') || !engine?.getResizeMode) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Are we able to check the UNIMPLEMENTED value from Capacitor instead of checking the platform?

So something like this:

try {
  return engine.getResizeMode();
} catch(e) {
  if (e.code === 'UNIMPLEMENTED') {
    return Promise.resolve(undefined); 
  }
  
  // throw the error unchanged if not an unimplemented error
  throw e;
}

My concern with the proposed approach is we are coupling our code to implementation details of the Capacitor plugin. Additionally, isPlatform relies on user agent sniffing which can be spoofed, so that check may not always be correct. Checking the unimplemented state lets us rely on Capacitor to tell us about functionality.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ooh that's brilliant, I didn't think of that approach!

Yes, we can do something very similar to this. I ended up using a .catch on the original promise, so when it is chained in the implementation, it will resolve undefined. It'll swallow the error in the client runtime, but still indicate the unimplemented method invocation in the native bridge logs.

This is a slightly different net result that the original implementation, which doesn't log the console.error for the unimplemented method invocation (because it isn't executed), but solves the original problem of the uncaught error.

CleanShot 2023-05-23 at 23 14 12@2x

@sean-perkins
sean-perkins requested a review from liamdebeasi May 24, 2023 03:17

@liamdebeasi liamdebeasi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

One comment, but otherwise looks good. Thanks for fixing this!

Comment thread core/src/utils/native/keyboard.ts Resolved
@sean-perkins sean-perkins changed the title fix(core): Keyboard resize mode is only called on iOS fix(core): Handle uncaught native keyboard exceptions May 24, 2023
@liamdebeasi liamdebeasi changed the title fix(core): Handle uncaught native keyboard exceptions fix(core): handle uncaught native keyboard exceptions May 24, 2023
@sean-perkins
sean-perkins added this pull request to the merge queue May 24, 2023
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 24, 2023
@sean-perkins
sean-perkins added this pull request to the merge queue May 24, 2023
Merged via the queue into main with commit 0e7359c May 24, 2023
@sean-perkins
sean-perkins deleted the sp/keyboard-resize-mode branch May 24, 2023 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: core @ionic/core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Ionic call Keyboard.getResizeMode() on Android throws uncaught error

3 participants