From 174172b6c64dc9117c48ed0e20c25e0b6c2fb625 Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Thu, 21 Jul 2022 03:26:43 -0700 Subject: [PATCH] feat(android): Add Optional Data Param for Error Object (#5719) Co-authored-by: jcesarmobile --- .../java/com/getcapacitor/PluginCall.java | 37 ++++++++++++++----- core/src/util.ts | 10 ++++- .../Capacitor/PluginCallResult.swift | 2 +- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java b/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java index c8b556ae05..6e8e48f95a 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java +++ b/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java @@ -96,7 +96,7 @@ public void errorCallback(String msg) { */ @Deprecated public void error(String msg, Exception ex) { - reject(msg, null, ex); + reject(msg, ex); } /** @@ -114,10 +114,10 @@ public void error(String msg, String code, Exception ex) { */ @Deprecated public void error(String msg) { - reject(msg, null, null); + reject(msg); } - public void reject(String msg, String code, Exception ex) { + public void reject(String msg, String code, Exception ex, JSObject data) { PluginResult errorResult = new PluginResult(); if (ex != null) { @@ -127,23 +127,42 @@ public void reject(String msg, String code, Exception ex) { try { errorResult.put("message", msg); errorResult.put("code", code); + if (null != data) { + errorResult.put("data", data); + } } catch (Exception jsonEx) { - Logger.error(Logger.tags("Plugin"), jsonEx.getMessage(), null); + Logger.error(Logger.tags("Plugin"), jsonEx.getMessage(), jsonEx); } this.msgHandler.sendResponseMessage(this, null, errorResult); } + public void reject(String msg, Exception ex, JSObject data) { + reject(msg, null, ex, data); + } + + public void reject(String msg, String code, JSObject data) { + reject(msg, code, null, data); + } + + public void reject(String msg, String code, Exception ex) { + reject(msg, code, ex, null); + } + + public void reject(String msg, JSObject data) { + reject(msg, null, null, data); + } + public void reject(String msg, Exception ex) { - reject(msg, null, ex); + reject(msg, null, ex, null); } public void reject(String msg, String code) { - reject(msg, code, null); + reject(msg, code, null, null); } public void reject(String msg) { - reject(msg, null, null); + reject(msg, null, null, null); } public void unimplemented() { @@ -151,7 +170,7 @@ public void unimplemented() { } public void unimplemented(String msg) { - reject(msg, "UNIMPLEMENTED", null); + reject(msg, "UNIMPLEMENTED", null, null); } public void unavailable() { @@ -159,7 +178,7 @@ public void unavailable() { } public void unavailable(String msg) { - reject(msg, "UNAVAILABLE", null); + reject(msg, "UNAVAILABLE", null, null); } public String getPluginId() { diff --git a/core/src/util.ts b/core/src/util.ts index 80b0619888..a4063b0f08 100644 --- a/core/src/util.ts +++ b/core/src/util.ts @@ -19,8 +19,16 @@ export enum ExceptionCode { Unavailable = 'UNAVAILABLE', } +export interface ExceptionData { + [key: string]: any; +} + export class CapacitorException extends Error { - constructor(readonly message: string, readonly code?: ExceptionCode) { + constructor( + readonly message: string, + readonly code?: ExceptionCode, + readonly data?: ExceptionData, + ) { super(message); } } diff --git a/ios/Capacitor/Capacitor/PluginCallResult.swift b/ios/Capacitor/Capacitor/PluginCallResult.swift index 7e02daf3ab..a426bbcb30 100644 --- a/ios/Capacitor/Capacitor/PluginCallResult.swift +++ b/ios/Capacitor/Capacitor/PluginCallResult.swift @@ -98,7 +98,7 @@ public enum PluginCallResult { self.code = code self.error = error if let data = data { - resultData = .dictionary(data) + resultData = .dictionary(["data": data]) } else { resultData = nil }