Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): Add Optional Data Param for Error Object #5719

Merged
merged 9 commits into from
Jul 21, 2022
37 changes: 28 additions & 9 deletions android/capacitor/src/main/java/com/getcapacitor/PluginCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void errorCallback(String msg) {
*/
@Deprecated
public void error(String msg, Exception ex) {
reject(msg, null, ex);
reject(msg, ex);
}

/**
Expand All @@ -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) {
Expand All @@ -127,39 +127,58 @@ 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() {
unimplemented("not implemented");
}

public void unimplemented(String msg) {
reject(msg, "UNIMPLEMENTED", null);
reject(msg, "UNIMPLEMENTED", null, null);
}

public void unavailable() {
unavailable("not available");
}

public void unavailable(String msg) {
reject(msg, "UNAVAILABLE", null);
reject(msg, "UNAVAILABLE", null, null);
}

public String getPluginId() {
Expand Down
10 changes: 9 additions & 1 deletion core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Capacitor/Capacitor/PluginCallResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down