Skip to content

Commit f93c354

Browse files
authored
feat: Allow plugins to reject with a string code (#2533)
1 parent d1009bb commit f93c354

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

android/capacitor/src/main/java/com/getcapacitor/PluginCall.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public void errorCallback(String msg) {
8484
}
8585

8686
public void error(String msg, Exception ex) {
87+
error(msg, null, ex);
88+
}
89+
90+
public void error(String msg, String code, Exception ex) {
8791
PluginResult errorResult = new PluginResult();
8892

8993
if(ex != null) {
@@ -92,6 +96,7 @@ public void error(String msg, Exception ex) {
9296

9397
try {
9498
errorResult.put("message", msg);
99+
errorResult.put("code", code);
95100
} catch (Exception jsonEx) {
96101
Log.e(LogUtils.getPluginTag(), jsonEx.getMessage());
97102
}
@@ -107,6 +112,10 @@ public void reject(String msg, Exception ex) {
107112
error(msg, ex);
108113
}
109114

115+
public void reject(String msg, String code) {
116+
error(msg, code, null);
117+
}
118+
110119
public void reject(String msg) {
111120
error(msg, null);
112121
}

ios/Capacitor/Capacitor/CAPBridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ enum BridgeError: Error {
418418
}
419419
}, error: {(error: CAPPluginCallError?) -> Void in
420420
let description = error?.error?.localizedDescription ?? ""
421-
self.toJsError(error: JSResultError(call: call, message: error!.message, errorMessage: description, error: error!.data))
421+
self.toJsError(error: JSResultError(call: call, message: error!.message, errorMessage: description, error: error!.data, code: error!.code))
422422
})!
423423

424424
plugin.perform(selector, with: pluginCall)

ios/Capacitor/Capacitor/CAPPluginCall.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
@interface CAPPluginCallError : NSObject
1111

1212
@property (nonatomic, strong) NSString *message;
13+
@property (nonatomic, strong) NSString *code;
1314
@property (nonatomic, strong) NSError *error;
1415
@property (nonatomic, strong) NSDictionary<NSString *, id> *data;
1516

16-
- (instancetype)initWithMessage:(NSString *)message error:(NSError *)error data:(NSDictionary<NSString *, id>*)data;
17+
- (instancetype)initWithMessage:(NSString *)message code:(NSString *)code error:(NSError *)error data:(NSDictionary<NSString *, id>*)data;
1718

1819
@end
1920

ios/Capacitor/Capacitor/CAPPluginCall.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ - (instancetype)init:(NSDictionary<NSString *, id>*)data {
1111

1212
@implementation CAPPluginCallError
1313

14-
- (instancetype)initWithMessage:(NSString *)message error:(NSError *)error data:(NSDictionary<NSString *,id> *)data {
14+
- (instancetype)initWithMessage:(NSString *)message code:(NSString *) code error:(NSError *)error data:(NSDictionary<NSString *,id> *)data {
1515
self.message = message;
16+
self.code = code;
1617
self.error = error;
1718
self.data = data;
1819
return self;

ios/Capacitor/Capacitor/CAPPluginCall.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ public typealias PluginEventListener = CAPPluginCall
7979
}
8080

8181
func error(_ message: String, _ error: Error? = nil, _ data: PluginCallErrorData = [:]) {
82-
errorHandler(CAPPluginCallError(message: message, error: error, data: data))
82+
errorHandler(CAPPluginCallError(message: message, code: nil, error: error, data: data))
8383
}
84-
85-
func reject(_ message: String, _ error: Error? = nil, _ data: PluginCallErrorData = [:]) {
86-
errorHandler(CAPPluginCallError(message: message, error: error, data: data))
84+
85+
func reject(_ message: String, _ code: String? = nil, _ error: Error? = nil, _ data: PluginCallErrorData = [:]) {
86+
errorHandler(CAPPluginCallError(message: message, code: code, error: error, data: data))
8787
}
8888

8989
func unimplemented() {
90-
errorHandler(CAPPluginCallError(message: CAPPluginCall.UNIMPLEMENTED, error: nil, data: [:]))
90+
errorHandler(CAPPluginCallError(message: CAPPluginCall.UNIMPLEMENTED, code: nil, error: nil, data: [:]))
9191
}
9292
}
9393

ios/Capacitor/Capacitor/JS.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@ public class JSResultError {
7878
var call: JSCall
7979
var error: JSResultBody
8080
var message: String
81+
var code: String?
8182
var errorMessage: String
82-
83-
public init(call: JSCall, message: String, errorMessage: String, error: JSResultBody) {
83+
84+
public init(call: JSCall, message: String, errorMessage: String, error: JSResultBody, code: String? = nil) {
8485
self.call = call
8586
self.message = message
8687
self.errorMessage = errorMessage
8788
self.error = error
89+
self.code = code
8890
}
89-
91+
9092
/**
9193
* Return a linkable error that we can use to help users find help for common exceptions,
9294
* much like AngularJS back in the day.
@@ -103,6 +105,7 @@ public class JSResultError {
103105
var jsonResponse = "{}"
104106

105107
error["message"] = self.message
108+
error["code"] = self.code
106109
error["errorMessage"] = self.errorMessage
107110
//error["_exlink"] = getLinkableError(self.message)
108111

0 commit comments

Comments
 (0)