Skip to content

Commit

Permalink
CDAP-21061: wrap all plugin methods to throw WrappedStageException
Browse files Browse the repository at this point in the history
  • Loading branch information
itsankit-google committed Aug 29, 2024
1 parent f5f8cb5 commit 2d061be
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ public WrappedStageException(Throwable cause, String stageName) {
public String getStageName() {
return stageName;
}

/**
* Returns the detail message string of this exception.
*
* @return the detail message as a {@String}.
*/
@Override
public String getMessage() {
return String.format("Stage '%s' encountered : %s", stageName, super.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright © 2024 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.cdap.etl.common.plugin;

import io.cdap.cdap.api.exception.WrappedStageException;
import java.util.concurrent.Callable;

/**
* A caller wrapper that catches exceptions thrown during execution of a callable
* and wraps them in a {@link WrappedStageException}.
* This class is primarily used to associate the exception with a specific stage name in a pipeline,
* helping in better debugging and error tracking.
*
* <p>
* The class delegates the actual calling operation to another {@link Caller} instance and
* ensures that any exceptions thrown are caught and rethrown as a {@link WrappedStageException},
* which includes the stage name where the error occurred.
* </p>
*/
public class ExceptionWrappingCaller extends Caller {
private final Caller delegate;
private final String stageName;

/**
* Constructs an ExceptionWrappingCaller.
*
* @param delegate The {@link Caller} instance that performs the actual calling of the callable.
* @param stageName The name of the stage associated with the exception,
* for easier identification of where the error occurred.
*/
public ExceptionWrappingCaller(Caller delegate, String stageName) {
this.delegate = delegate;
this.stageName = stageName;
}

/**
* Executes the given {@link Callable}, wrapping any exceptions thrown
* during execution in a {@link WrappedStageException}.
*
* @param callable The callable task to execute.
* @param <T> The return type of the callable.
* @return The result of the callable task.
* @throws Exception if an exception occurs during the callable execution.
*/
@Override
public <T> T call(Callable<T> callable) throws Exception {
try {
return delegate.call(callable);
} catch (Exception e) {
throw new WrappedStageException(e, stageName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private Object wrapPlugin(String pluginId, Object plugin) {
}

public Caller getCaller(String pluginId) {

Check warning on line 117 in cdap-app-templates/cdap-etl/cdap-etl-core/src/main/java/io/cdap/cdap/etl/common/plugin/PipelinePluginContext.java

View workflow job for this annotation

GitHub Actions / Checkstyle

com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck

Missing a Javadoc comment.
Caller caller = Caller.DEFAULT;
Caller caller = new ExceptionWrappingCaller(Caller.DEFAULT, pluginId);
if (stageLoggingEnabled) {
caller = StageLoggingCaller.wrap(caller, pluginId);
}
Expand Down

0 comments on commit 2d061be

Please sign in to comment.