Skip to content

Commit

Permalink
fix(core): handle uncaught native keyboard exceptions (#27514)
Browse files Browse the repository at this point in the history
Issue number: Resolves #27503

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

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?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

- 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
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Dev-build: `7.0.8-dev.11684444351.1b1ab142` (outdated)
  • Loading branch information
sean-perkins committed May 24, 2023
1 parent 01f9959 commit 0e7359c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 11 additions & 3 deletions core/src/utils/native/keyboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { win } from '../browser';

import type { NativePluginError } from './native-interface';

// Interfaces source: https://capacitorjs.com/docs/apis/keyboard#interfaces
export interface KeyboardResizeOptions {
mode: KeyboardResize;
Expand All @@ -18,10 +20,16 @@ export const Keyboard = {
},
getResizeMode(): Promise<KeyboardResizeOptions | undefined> {
const engine = this.getEngine();
if (!engine || !engine.getResizeMode) {
if (!engine?.getResizeMode) {
return Promise.resolve(undefined);
}

return engine.getResizeMode();
return engine.getResizeMode().catch((e: NativePluginError) => {
if (e.code === 'UNIMPLEMENTED') {
// If the native implementation is not available
// we treat it the same as if the plugin is not available.
return undefined;
}
throw e;
});
},
};
13 changes: 13 additions & 0 deletions core/src/utils/native/native-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Used to represent a generic error from a native plugin call.
*/
export interface NativePluginError {
/**
* The error code.
*/
code?: string;
/**
* The error message.
*/
message?: string;
}

0 comments on commit 0e7359c

Please sign in to comment.