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

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)
This commit is contained in:
Sean Perkins
2023-05-24 17:15:11 -04:00
committed by GitHub
parent 01f99597f7
commit 0e7359c07f
2 changed files with 24 additions and 3 deletions

View File

@ -1,5 +1,7 @@
import { win } from '../browser'; import { win } from '../browser';
import type { NativePluginError } from './native-interface';
// Interfaces source: https://capacitorjs.com/docs/apis/keyboard#interfaces // Interfaces source: https://capacitorjs.com/docs/apis/keyboard#interfaces
export interface KeyboardResizeOptions { export interface KeyboardResizeOptions {
mode: KeyboardResize; mode: KeyboardResize;
@ -18,10 +20,16 @@ export const Keyboard = {
}, },
getResizeMode(): Promise<KeyboardResizeOptions | undefined> { getResizeMode(): Promise<KeyboardResizeOptions | undefined> {
const engine = this.getEngine(); const engine = this.getEngine();
if (!engine || !engine.getResizeMode) { if (!engine?.getResizeMode) {
return Promise.resolve(undefined); return Promise.resolve(undefined);
} }
return engine.getResizeMode().catch((e: NativePluginError) => {
return engine.getResizeMode(); 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;
});
}, },
}; };

View File

@ -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;
}