From 0e7359c07f53eccb362ff2bf331396c0376ba6f3 Mon Sep 17 00:00:00 2001 From: Sean Perkins Date: Wed, 24 May 2023 17:15:11 -0400 Subject: [PATCH] fix(core): handle uncaught native keyboard exceptions (#27514) 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 - [x] No ## Other information Dev-build: `7.0.8-dev.11684444351.1b1ab142` (outdated) --- core/src/utils/native/keyboard.ts | 14 +++++++++++--- core/src/utils/native/native-interface.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 core/src/utils/native/native-interface.ts diff --git a/core/src/utils/native/keyboard.ts b/core/src/utils/native/keyboard.ts index 7325b2bee98..6a4e88efbe7 100644 --- a/core/src/utils/native/keyboard.ts +++ b/core/src/utils/native/keyboard.ts @@ -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; @@ -18,10 +20,16 @@ export const Keyboard = { }, getResizeMode(): Promise { 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; + }); }, }; diff --git a/core/src/utils/native/native-interface.ts b/core/src/utils/native/native-interface.ts new file mode 100644 index 00000000000..a486f455aa0 --- /dev/null +++ b/core/src/utils/native/native-interface.ts @@ -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; +}