From f8a09fa82eefb83a23877a89cbbea4069c11c137 Mon Sep 17 00:00:00 2001 From: psainics Date: Tue, 18 Feb 2025 10:19:29 +0530 Subject: [PATCH] Error management for javascript plugin --- .../plugin/transform/JavaScriptTransform.java | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/core-plugins/src/main/java/io/cdap/plugin/transform/JavaScriptTransform.java b/core-plugins/src/main/java/io/cdap/plugin/transform/JavaScriptTransform.java index 4d79cc30e..35e11c53c 100644 --- a/core-plugins/src/main/java/io/cdap/plugin/transform/JavaScriptTransform.java +++ b/core-plugins/src/main/java/io/cdap/plugin/transform/JavaScriptTransform.java @@ -28,6 +28,9 @@ import io.cdap.cdap.api.annotation.Plugin; import io.cdap.cdap.api.data.format.StructuredRecord; import io.cdap.cdap.api.data.schema.Schema; +import io.cdap.cdap.api.exception.ErrorCategory; +import io.cdap.cdap.api.exception.ErrorType; +import io.cdap.cdap.api.exception.ErrorUtils; import io.cdap.cdap.api.plugin.PluginConfig; import io.cdap.cdap.etl.api.Arguments; import io.cdap.cdap.etl.api.Emitter; @@ -185,9 +188,10 @@ public void initialize(TransformContext context) throws Exception { Class somClass = Class.forName("jdk.nashorn.api.scripting.ScriptObjectMirror"); somValuesMethod = somClass.getMethod("values"); } catch (NoSuchMethodException e) { - throw new RuntimeException( - "Failed to get method ScriptObjectMirror#values() for converting ScriptObjectMirror to List. " + - "Please check your version of Nashorn is supported.", e); + String error = "Failed to get method ScriptObjectMirror#values() for converting ScriptObjectMirror to List." + + " Please check your version of Nashorn is supported."; + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.SYSTEM, false, e); } catch (ClassNotFoundException e) { // Ignore -- we don't have Nashorn, so no need to handle Nashorn } @@ -208,7 +212,9 @@ public void transform(StructuredRecord input, Emitter emitter) engine.put(EMITTER_NAME, jsEmitter); invocable.invokeFunction(FUNCTION_NAME); } catch (Exception e) { - throw new IllegalArgumentException("Could not transform input: " + e.getMessage(), e); + String error = String.format("Could not transform input: %s", e.getMessage()); + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.USER, false, e); } } @@ -265,7 +271,9 @@ private InvalidEntry getErrorObject(Map result, StructuredReco "errorCode must be a valid Integer"); errorCodeInt = errorCodeDouble.intValue(); } else { - throw new IllegalArgumentException("Unsupported errorCode type: " + errorCode.getClass().getName()); + String error = String.format("Unsupported errorCode type: %s", errorCode.getClass().getName()); + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.USER, false, null); } return new InvalidEntry<>(errorCodeInt, (String) result.get("errorMsg"), input); } @@ -313,8 +321,9 @@ private Object decode(Object object, Schema schema) { case UNION: return decodeUnion(object, schema.getUnionSchemas()); } - - throw new RuntimeException("Unable decode object with schema " + schema); + String error = String.format("Unable decode object with schema %s", schema); + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.USER, false, null); } private StructuredRecord decodeRecord(Map nativeObject, Schema schema) { @@ -333,7 +342,9 @@ private List jsObject2List(Object object) { try { return (List) somValuesMethod.invoke(object); } catch (InvocationTargetException | IllegalAccessException | ClassCastException e) { - throw new RuntimeException("Failed to convert ScriptObjectMirror to List", e); + String error = "Failed to convert ScriptObjectMirror to List"; + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.USER, false, null); } } return (List) object; @@ -369,7 +380,9 @@ private Object decodeSimpleType(Object object, Schema schema) { case STRING: return (String) object; } - throw new RuntimeException("Unable decode object with schema " + schema); + String error = String.format("Unable decode object with schema %s", schema); + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.USER, false, null); } private Map decodeMap(Map object, Schema keySchema, Schema valSchema) { @@ -396,7 +409,9 @@ private Object decodeUnion(Object object, List schemas) { // could be ok, just move on and try the next schema } } - throw new RuntimeException("Unable decode union with schema " + schemas); + String error = String.format("Unable decode union with schema %s", schema); + throw ErrorUtils.getProgramFailureException(new ErrorCategory(ErrorCategory.ErrorCategoryEnum.PLUGIN), + error, error, ErrorType.USER, false, null); } private void init(@Nullable TransformContext context, FailureCollector collector) {