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

[CDAP-21061] wrap all plugin methods to throw WrappedStageException #15690

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -47,7 +47,7 @@
private final boolean stageLoggingEnabled;
private final boolean processTimingEnabled;

public PipelinePluginContext(PluginContext delegate, Metrics metrics,

Check warning on line 50 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.
boolean stageLoggingEnabled, boolean processTimingEnabled) {
this.delegate = delegate;
this.metrics = metrics;
Expand Down Expand Up @@ -114,8 +114,8 @@
return wrapUnknownPlugin(pluginId, plugin, caller);
}

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
Loading