Skip to content

Commit

Permalink
Initialize cause of some InterruptedExceptions
Browse files Browse the repository at this point in the history
Pack an OperationCanceledException inside the InterruptedException when
this is thrown due to the user canceling an operation. This makes it
easier to determine the cause of the InterruptedException from outside
the methods (i.e. in the catch-clauses). Adapt the method
ModalContext::checkCanceled accordingly. Change
WorkspaceModifyOperation::run so it preserves the original
exceptions if possible.

Same idea as
#1259
  • Loading branch information
fedejeanne committed Nov 13, 2023
1 parent f4f59ce commit 12615e9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,11 @@ public static boolean canProgressMonitorBeUsed(IProgressMonitor monitor1, IProgr
* </p>
*
* <pre>
* if (monitor.isCanceled())
* throw new InterruptedException();
* if (monitor.isCanceled()) {
* InterruptedException interruptedException = new InterruptedException();
* interruptedException.initCause(new OperationCanceledException());
* throw interruptedException;
* }
* </pre>
*
*
Expand All @@ -255,7 +258,9 @@ public static boolean canProgressMonitorBeUsed(IProgressMonitor monitor1, IProgr
*/
public static void checkCanceled(IProgressMonitor monitor) throws InterruptedException {
if (monitor.isCanceled()) {
throw new InterruptedException();
InterruptedException interruptedException = new InterruptedException();
interruptedException.initCause(new OperationCanceledException());
throw interruptedException;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Iterator;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.OperationCanceledException;

/**
* This object maintains a collection of elements, sorted by a comparator
Expand Down Expand Up @@ -386,7 +387,9 @@ private final int partition(int subTree, FastProgressReporter mon) throws Interr
}

if (mon.isCanceled()) {
throw new InterruptedException();
InterruptedException interruptedException = new InterruptedException();
interruptedException.initCause(new OperationCanceledException());
throw interruptedException;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.ui.actions;

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
Expand Down Expand Up @@ -103,20 +104,17 @@ protected abstract void execute(IProgressMonitor monitor)
@Override
public synchronized final void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
final InvocationTargetException[] iteHolder = new InvocationTargetException[1];
final AtomicReference<Exception> exceptionHolder = new AtomicReference<>();
try {
IWorkspaceRunnable workspaceRunnable = pm -> {
try {
execute(pm);
} catch (InvocationTargetException e1) {
} catch (InvocationTargetException | InterruptedException e) {
// Pass it outside the workspace runnable
iteHolder[0] = e1;
} catch (InterruptedException e2) {
// Re-throw as OperationCanceledException, which will be
// caught and re-thrown as InterruptedException below.
throw new OperationCanceledException(e2.getMessage());
exceptionHolder.set(e);
}
// CoreException and OperationCanceledException are propagated
// CoreException and unchecked exceptions (e.g. OperationCanceledException) are
// propagated to the outer catch
};
// if we are in the UI thread, make sure we use progress monitor
// that spins event loop to allow processing of pending asyncExecs
Expand All @@ -137,9 +135,12 @@ public synchronized final void run(IProgressMonitor monitor)
interruptedException.initCause(e);
throw interruptedException;
}
// Re-throw the InvocationTargetException, if any occurred
if (iteHolder[0] != null) {
throw iteHolder[0];

// Re-throw any exceptions caught while running the IWorkspaceRunnable
if (exceptionHolder.get() instanceof InvocationTargetException ite) {
throw ite;
} else if (exceptionHolder.get() instanceof InterruptedException ie) {
throw ie;
}
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;
Expand Down Expand Up @@ -227,7 +228,9 @@ protected void exportFile(IFile file, IPath location)
.queryOverwrite(properPathString);

if (overwriteAnswer.equals(IOverwriteQuery.CANCEL)) {
throw new InterruptedException();
InterruptedException interruptedException = new InterruptedException();
interruptedException.initCause(new OperationCanceledException());
throw interruptedException;
}

if (overwriteAnswer.equals(IOverwriteQuery.NO)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;
import org.eclipse.jface.util.BidiUtils;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
Expand Down Expand Up @@ -1122,9 +1123,7 @@ public void filterElements(Collection files,
}
Iterator filesList = files.iterator();
while (filesList.hasNext()) {
if (monitor.isCanceled()) {
throw new InterruptedException();
}
ModalContext.checkCanceled(monitor);
checkFile(filesList.next());
}
}
Expand Down

0 comments on commit 12615e9

Please sign in to comment.