From f93c35481afeade8fcd5689a5aa605650455dfcd Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Mon, 9 Mar 2020 12:00:49 +0100 Subject: [PATCH] feat: Allow plugins to reject with a string code (#2533) --- .../src/main/java/com/getcapacitor/PluginCall.java | 9 +++++++++ ios/Capacitor/Capacitor/CAPBridge.swift | 2 +- ios/Capacitor/Capacitor/CAPPluginCall.h | 3 ++- ios/Capacitor/Capacitor/CAPPluginCall.m | 3 ++- ios/Capacitor/Capacitor/CAPPluginCall.swift | 10 +++++----- ios/Capacitor/Capacitor/JS.swift | 9 ++++++--- 6 files changed, 25 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 d3bbac9393..61ee156f0d 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java +++ b/android/capacitor/src/main/java/com/getcapacitor/PluginCall.java @@ -84,6 +84,10 @@ public void errorCallback(String msg) { } public void error(String msg, Exception ex) { + error(msg, null, ex); + } + + public void error(String msg, String code, Exception ex) { PluginResult errorResult = new PluginResult(); if(ex != null) { @@ -92,6 +96,7 @@ public void error(String msg, Exception ex) { try { errorResult.put("message", msg); + errorResult.put("code", code); } catch (Exception jsonEx) { Log.e(LogUtils.getPluginTag(), jsonEx.getMessage()); } @@ -107,6 +112,10 @@ public void reject(String msg, Exception ex) { error(msg, ex); } + public void reject(String msg, String code) { + error(msg, code, null); + } + public void reject(String msg) { error(msg, null); } diff --git a/ios/Capacitor/Capacitor/CAPBridge.swift b/ios/Capacitor/Capacitor/CAPBridge.swift index e621bd9bac..3ab47aa676 100644 --- a/ios/Capacitor/Capacitor/CAPBridge.swift +++ b/ios/Capacitor/Capacitor/CAPBridge.swift @@ -418,7 +418,7 @@ enum BridgeError: Error { } }, error: {(error: CAPPluginCallError?) -> Void in let description = error?.error?.localizedDescription ?? "" - self.toJsError(error: JSResultError(call: call, message: error!.message, errorMessage: description, error: error!.data)) + self.toJsError(error: JSResultError(call: call, message: error!.message, errorMessage: description, error: error!.data, code: error!.code)) })! plugin.perform(selector, with: pluginCall) diff --git a/ios/Capacitor/Capacitor/CAPPluginCall.h b/ios/Capacitor/Capacitor/CAPPluginCall.h index 6979915326..5a8803d1bf 100644 --- a/ios/Capacitor/Capacitor/CAPPluginCall.h +++ b/ios/Capacitor/Capacitor/CAPPluginCall.h @@ -10,10 +10,11 @@ @interface CAPPluginCallError : NSObject @property (nonatomic, strong) NSString *message; +@property (nonatomic, strong) NSString *code; @property (nonatomic, strong) NSError *error; @property (nonatomic, strong) NSDictionary *data; -- (instancetype)initWithMessage:(NSString *)message error:(NSError *)error data:(NSDictionary*)data; +- (instancetype)initWithMessage:(NSString *)message code:(NSString *)code error:(NSError *)error data:(NSDictionary*)data; @end diff --git a/ios/Capacitor/Capacitor/CAPPluginCall.m b/ios/Capacitor/Capacitor/CAPPluginCall.m index c7504340b6..196db228c8 100644 --- a/ios/Capacitor/Capacitor/CAPPluginCall.m +++ b/ios/Capacitor/Capacitor/CAPPluginCall.m @@ -11,8 +11,9 @@ - (instancetype)init:(NSDictionary*)data { @implementation CAPPluginCallError -- (instancetype)initWithMessage:(NSString *)message error:(NSError *)error data:(NSDictionary *)data { +- (instancetype)initWithMessage:(NSString *)message code:(NSString *) code error:(NSError *)error data:(NSDictionary *)data { self.message = message; + self.code = code; self.error = error; self.data = data; return self; diff --git a/ios/Capacitor/Capacitor/CAPPluginCall.swift b/ios/Capacitor/Capacitor/CAPPluginCall.swift index 518e2aa3ac..59a2289150 100644 --- a/ios/Capacitor/Capacitor/CAPPluginCall.swift +++ b/ios/Capacitor/Capacitor/CAPPluginCall.swift @@ -79,15 +79,15 @@ public typealias PluginEventListener = CAPPluginCall } func error(_ message: String, _ error: Error? = nil, _ data: PluginCallErrorData = [:]) { - errorHandler(CAPPluginCallError(message: message, error: error, data: data)) + errorHandler(CAPPluginCallError(message: message, code: nil, error: error, data: data)) } - - func reject(_ message: String, _ error: Error? = nil, _ data: PluginCallErrorData = [:]) { - errorHandler(CAPPluginCallError(message: message, error: error, data: data)) + + func reject(_ message: String, _ code: String? = nil, _ error: Error? = nil, _ data: PluginCallErrorData = [:]) { + errorHandler(CAPPluginCallError(message: message, code: code, error: error, data: data)) } func unimplemented() { - errorHandler(CAPPluginCallError(message: CAPPluginCall.UNIMPLEMENTED, error: nil, data: [:])) + errorHandler(CAPPluginCallError(message: CAPPluginCall.UNIMPLEMENTED, code: nil, error: nil, data: [:])) } } diff --git a/ios/Capacitor/Capacitor/JS.swift b/ios/Capacitor/Capacitor/JS.swift index 598e21eb14..ada4f4298b 100644 --- a/ios/Capacitor/Capacitor/JS.swift +++ b/ios/Capacitor/Capacitor/JS.swift @@ -78,15 +78,17 @@ public class JSResultError { var call: JSCall var error: JSResultBody var message: String + var code: String? var errorMessage: String - - public init(call: JSCall, message: String, errorMessage: String, error: JSResultBody) { + + public init(call: JSCall, message: String, errorMessage: String, error: JSResultBody, code: String? = nil) { self.call = call self.message = message self.errorMessage = errorMessage self.error = error + self.code = code } - + /** * Return a linkable error that we can use to help users find help for common exceptions, * much like AngularJS back in the day. @@ -103,6 +105,7 @@ public class JSResultError { var jsonResponse = "{}" error["message"] = self.message + error["code"] = self.code error["errorMessage"] = self.errorMessage //error["_exlink"] = getLinkableError(self.message)