Skip to content

Commit

Permalink
feat: Allow plugins to reject with a string code (#2533)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Mar 9, 2020
1 parent d1009bb commit f93c354
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Capacitor/Capacitor/CAPBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion ios/Capacitor/Capacitor/CAPPluginCall.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSString *, id> *data;

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

@end

Expand Down
3 changes: 2 additions & 1 deletion ios/Capacitor/Capacitor/CAPPluginCall.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ - (instancetype)init:(NSDictionary<NSString *, id>*)data {

@implementation CAPPluginCallError

- (instancetype)initWithMessage:(NSString *)message error:(NSError *)error data:(NSDictionary<NSString *,id> *)data {
- (instancetype)initWithMessage:(NSString *)message code:(NSString *) code error:(NSError *)error data:(NSDictionary<NSString *,id> *)data {
self.message = message;
self.code = code;
self.error = error;
self.data = data;
return self;
Expand Down
10 changes: 5 additions & 5 deletions ios/Capacitor/Capacitor/CAPPluginCall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [:]))
}
}

9 changes: 6 additions & 3 deletions ios/Capacitor/Capacitor/JS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down

0 comments on commit f93c354

Please sign in to comment.