From 4ca8b721676846f37126a950b12ab5207948e1d5 Mon Sep 17 00:00:00 2001 From: kaibocai <89094811+kaibocai@users.noreply.github.com> Date: Tue, 16 Aug 2022 09:42:54 -0500 Subject: [PATCH] enhance rpc exception in app inisght sdk --- .../worker/broker/JavaMethodInvokeInfo.java | 7 ++++++ .../exception/UserFunctionException.java | 25 +++++++++++++++++++ .../worker/exception/WorkerBaseException.java | 23 +++++++++++++++++ .../worker/handler/MessageHandler.java | 11 +++++++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/microsoft/azure/functions/worker/exception/UserFunctionException.java create mode 100644 src/main/java/com/microsoft/azure/functions/worker/exception/WorkerBaseException.java diff --git a/src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodInvokeInfo.java b/src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodInvokeInfo.java index a60615ef..9bc98101 100644 --- a/src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodInvokeInfo.java +++ b/src/main/java/com/microsoft/azure/functions/worker/broker/JavaMethodInvokeInfo.java @@ -2,7 +2,10 @@ import java.lang.reflect.*; import java.util.*; +import java.util.logging.Level; +import com.microsoft.azure.functions.worker.WorkerLogManager; +import com.microsoft.azure.functions.worker.exception.UserFunctionException; import org.apache.commons.lang3.exception.*; interface InstanceSupplier { @@ -18,8 +21,12 @@ private JavaMethodInvokeInfo() {} Object invoke(InstanceSupplier instanceSupplier) throws Exception { Object instance = Modifier.isStatic(this.m.getModifiers()) ? null : instanceSupplier.get(); + try { return this.m.invoke(instance, this.args); + } catch (InvocationTargetException ex) { + WorkerLogManager.getSystemLogger().severe(String.format("exception happens in customer function: %s : %s", this.m.getName(), ex.getMessage())); + return ExceptionUtils.rethrow(new UserFunctionException(this.m.getName(), ex.getCause())); } catch (Exception ex) { return ExceptionUtils.rethrow(ex); } diff --git a/src/main/java/com/microsoft/azure/functions/worker/exception/UserFunctionException.java b/src/main/java/com/microsoft/azure/functions/worker/exception/UserFunctionException.java new file mode 100644 index 00000000..435d8537 --- /dev/null +++ b/src/main/java/com/microsoft/azure/functions/worker/exception/UserFunctionException.java @@ -0,0 +1,25 @@ +package com.microsoft.azure.functions.worker.exception; + +public class UserFunctionException extends WorkerBaseException{ + + private static final String message = "exception happens in customer function: "; + public UserFunctionException() { + super(); + } + + public UserFunctionException(String message) { + super(message); + } + + public UserFunctionException(String input, Throwable cause) { + super(message + input, cause); + } + + public UserFunctionException(Throwable cause) { + super(cause); + } + + public UserFunctionException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/microsoft/azure/functions/worker/exception/WorkerBaseException.java b/src/main/java/com/microsoft/azure/functions/worker/exception/WorkerBaseException.java new file mode 100644 index 00000000..b23c3f78 --- /dev/null +++ b/src/main/java/com/microsoft/azure/functions/worker/exception/WorkerBaseException.java @@ -0,0 +1,23 @@ +package com.microsoft.azure.functions.worker.exception; + +public class WorkerBaseException extends RuntimeException{ + public WorkerBaseException() { + super(); + } + + public WorkerBaseException(String message) { + super(message); + } + + public WorkerBaseException(String message, Throwable cause) { + super(message, cause); + } + + public WorkerBaseException(Throwable cause) { + super(cause); + } + + public WorkerBaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/src/main/java/com/microsoft/azure/functions/worker/handler/MessageHandler.java b/src/main/java/com/microsoft/azure/functions/worker/handler/MessageHandler.java index 487d8f4b..3aded2e3 100644 --- a/src/main/java/com/microsoft/azure/functions/worker/handler/MessageHandler.java +++ b/src/main/java/com/microsoft/azure/functions/worker/handler/MessageHandler.java @@ -1,10 +1,12 @@ package com.microsoft.azure.functions.worker.handler; +import java.lang.reflect.InvocationTargetException; import java.util.concurrent.*; import java.util.function.*; import java.util.logging.*; import com.google.protobuf.*; +import com.microsoft.azure.functions.worker.exception.UserFunctionException; import org.apache.commons.lang3.*; import org.apache.commons.lang3.exception.*; @@ -46,10 +48,17 @@ public void handle() { if (statusMessage != null) { this.getLogger().info(statusMessage); } + } catch (UserFunctionException ex) { + status = StatusResult.Status.Failure; + statusMessage = ExceptionUtils.getRootCauseMessage(ex); + rpcException = RpcException.newBuilder().setMessage(statusMessage).setIsUserException(true) + .setType(ExceptionUtils.getRootCause(ex).getClass().getName()) + .setStackTrace(ExceptionUtils.getStackTrace(ex)).build(); } catch (Exception ex) { status = StatusResult.Status.Failure; statusMessage = ExceptionUtils.getRootCauseMessage(ex); - rpcException = RpcException.newBuilder().setMessage(statusMessage).setStackTrace(ExceptionUtils.getStackTrace(ex)).build(); + rpcException = RpcException.newBuilder().setMessage(statusMessage) + .setStackTrace(ExceptionUtils.getStackTrace(ex)).build(); } if (this.responseStatusMarshaller != null) { StatusResult.Builder result = StatusResult.newBuilder().setStatus(status).setResult(statusMessage);