Skip to content

Commit

Permalink
Sort out error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Mar 22, 2024
1 parent fef841b commit d5b3783
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.nio.file.Path;
import java.util.stream.Collectors;
import org.eclipse.aether.resolution.ArtifactDescriptorException;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -52,7 +51,10 @@ protected boolean doCall(Context context) throws Exception {
.map(g -> {
try {
return toolboxCommando.loadGav(g, boms);
} catch (ArtifactDescriptorException e) {
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.Artifacts;
import java.nio.file.Path;
import org.eclipse.aether.deployment.DeploymentException;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -44,7 +43,7 @@ public final class Deploy extends ResolverCommandSupport {
private Path javadoc;

@Override
protected boolean doCall(Context context) throws DeploymentException {
protected boolean doCall(Context context) throws Exception {
Artifacts artifacts = new Artifacts(gav);
artifacts.addMain(jar);
if (pom != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package eu.maveniverse.maven.toolbox.cli;

import eu.maveniverse.maven.mima.context.Context;
import org.eclipse.aether.deployment.DeploymentException;
import picocli.CommandLine;

/**
Expand All @@ -20,7 +19,7 @@ public final class DeployRecorded extends ResolverCommandSupport {
private String remoteRepositorySpec;

@Override
protected boolean doCall(Context context) throws DeploymentException {
protected boolean doCall(Context context) throws Exception {
return getToolboxCommando(context).deployAllRecorded(remoteRepositorySpec, true, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import eu.maveniverse.maven.mima.context.Context;
import eu.maveniverse.maven.toolbox.shared.Artifacts;
import java.nio.file.Path;
import org.eclipse.aether.installation.InstallationException;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -41,7 +40,7 @@ public final class Install extends ResolverCommandSupport {
private Path javadoc;

@Override
protected boolean doCall(Context context) throws InstallationException {
protected boolean doCall(Context context) throws Exception {
Artifacts artifacts = new Artifacts(gav);
artifacts.addMain(jar);
if (pom != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.util.stream.Collectors;
import org.eclipse.aether.resolution.ArtifactDescriptorException;
import picocli.CommandLine;

/**
Expand Down Expand Up @@ -60,7 +59,10 @@ protected boolean doCall(Context context) throws Exception {
.map(g -> {
try {
return toolboxCommando.loadGav(g, boms);
} catch (ArtifactDescriptorException e) {
} catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}
throw new RuntimeException(e);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

Expand All @@ -23,7 +21,7 @@ public class ClasspathMojo extends ProjectMojoSupport {
private String scope;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws MojoExecutionException, MojoFailureException {
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.classpath(ResolutionScope.parse(scope), projectAsResolutionRoot(), output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import eu.maveniverse.maven.toolbox.shared.ResolutionRoot;
import eu.maveniverse.maven.toolbox.shared.Slf4jOutput;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.IOException;
import java.util.stream.Collectors;
import org.apache.maven.RepositoryUtils;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
Expand Down Expand Up @@ -69,9 +70,12 @@ public final void execute() throws MojoExecutionException, MojoFailureException
Runtime runtime = Runtimes.INSTANCE.getRuntime();
try (Context context = runtime.create(ContextOverrides.create().build())) {
doExecute(ToolboxCommando.create(runtime, context));
} catch (RuntimeException | IOException e) {
throw new MojoFailureException(e);
} catch (Exception e) {
throw new MojoExecutionException(e);
}
}

protected abstract void doExecute(ToolboxCommando toolboxCommando)
throws MojoExecutionException, MojoFailureException;
protected abstract void doExecute(ToolboxCommando toolboxCommando) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

Expand All @@ -29,7 +27,7 @@ public class TreeMojo extends ProjectMojoSupport {
private boolean verboseTree;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws MojoExecutionException, MojoFailureException {
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.tree(ResolutionScope.parse(scope), projectAsResolutionRoot(), verboseTree, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.resolution.ArtifactDescriptorException;

@Mojo(name = "gav-classpath", requiresProject = false, threadSafe = true)
public class GavClasspathMojo extends GavMojoSupport {
Expand All @@ -37,13 +34,7 @@ public class GavClasspathMojo extends GavMojoSupport {
private String boms;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws MojoExecutionException, MojoFailureException {
try {
toolboxCommando.classpath(ResolutionScope.parse(scope), toolboxCommando.loadGav(gav, csv(boms)), output);
} catch (RuntimeException e) {
throw new MojoExecutionException(e);
} catch (ArtifactDescriptorException e) {
throw new MojoFailureException(e);
}
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.classpath(ResolutionScope.parse(scope), toolboxCommando.loadGav(gav, csv(boms)), output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
package eu.maveniverse.maven.toolbox.plugin.gav;

import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.IOException;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

Expand All @@ -22,11 +20,7 @@ public class GavListMojo extends GavSearchMojoSupport {
private String gavoid;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws MojoExecutionException {
try {
toolboxCommando.list(getRemoteRepository(toolboxCommando), gavoid, output);
} catch (IOException e) {
throw new MojoExecutionException(e);
}
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.list(getRemoteRepository(toolboxCommando), gavoid, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import eu.maveniverse.maven.toolbox.shared.Output;
import eu.maveniverse.maven.toolbox.shared.Slf4jOutput;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -32,11 +33,14 @@ public final void execute() throws MojoExecutionException, MojoFailureException
Runtime runtime = Runtimes.INSTANCE.getRuntime();
try (Context context = runtime.create(ContextOverrides.create().build())) {
doExecute(ToolboxCommando.create(runtime, context));
} catch (RuntimeException | IOException e) {
throw new MojoFailureException(e);
} catch (Exception e) {
throw new MojoExecutionException(e);
}
}

protected abstract void doExecute(ToolboxCommando toolboxCommando)
throws MojoExecutionException, MojoFailureException;
protected abstract void doExecute(ToolboxCommando toolboxCommando) throws Exception;

protected Collection<String> csv(String csv) {
if (csv == null || csv.trim().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

import eu.maveniverse.maven.toolbox.shared.ResolutionScope;
import eu.maveniverse.maven.toolbox.shared.ToolboxCommando;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.eclipse.aether.resolution.ArtifactDescriptorException;

@Mojo(name = "gav-tree", requiresProject = false, threadSafe = true)
public class GavTreeMojo extends GavMojoSupport {
Expand Down Expand Up @@ -43,14 +40,8 @@ public class GavTreeMojo extends GavMojoSupport {
private String boms;

@Override
protected void doExecute(ToolboxCommando toolboxCommando) throws MojoExecutionException, MojoFailureException {
try {
toolboxCommando.tree(
ResolutionScope.parse(scope), toolboxCommando.loadGav(gav, csv(boms)), verboseTree, output);
} catch (RuntimeException e) {
throw new MojoExecutionException(e);
} catch (ArtifactDescriptorException e) {
throw new MojoFailureException(e);
}
protected void doExecute(ToolboxCommando toolboxCommando) throws Exception {
toolboxCommando.tree(
ResolutionScope.parse(scope), toolboxCommando.loadGav(gav, csv(boms)), verboseTree, output);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@
import java.util.function.Supplier;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.eclipse.aether.resolution.ArtifactDescriptorException;

/**
* The Toolbox Commando, that implements all the commands that are exposed via Mojos or CLI.
* <p>
* This instance manages {@link Context}, corresponding {@link ToolboxResolver} and maps one-to-one onto commands.
* Can be considered something like "high level" API of Toolbox.
* This instance manages {@link Context}, corresponding {@link ToolboxResolver} and {@link ToolboxSearchApi}
* and maps one-to-one onto commands. Can be considered something like "high level" API of Toolbox.
* <p>
* Note on error handling: each "commando" method is marked to throw and returns a {@link boolean}.
* If method cleanly returns, the result shows the "successful" outcome of command. If method throws,
* {@link RuntimeException} instances (for example NPE, IAEx, ISEx) mark "bad input", or configuration
* errors. The checked exception instances on the other hand come from corresponding subsystem like
* resolver is.
*/
public interface ToolboxCommando extends Closeable {
/**
Expand All @@ -53,50 +58,56 @@ static ToolboxCommando create(Runtime runtime, Context context) {
/**
* Shorthand method, creates {@link ResolutionRoot} our of passed in artifact.
*/
default ResolutionRoot loadGav(String gav) throws ArtifactDescriptorException {
default ResolutionRoot loadGav(String gav) throws Exception {
return loadGav(gav, Collections.emptyList());
}

/**
* Shorthand method, creates {@link ResolutionRoot} our of passed in artifact and BOMs.
*/
ResolutionRoot loadGav(String gav, Collection<String> boms) throws ArtifactDescriptorException;
ResolutionRoot loadGav(String gav, Collection<String> boms) throws Exception;

boolean classpath(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, Output output);
boolean classpath(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, Output output) throws Exception;

boolean copy(Collection<Artifact> artifacts, ArtifactSink sink, Output output);
boolean copy(Collection<Artifact> artifacts, ArtifactSink sink, Output output) throws Exception;

boolean copyTransitive(
ResolutionScope resolutionScope,
Collection<ResolutionRoot> resolutionRoots,
ArtifactSink sink,
Output output);
Output output)
throws Exception;

boolean deploy(String remoteRepositorySpec, Supplier<Collection<Artifact>> artifactSupplier, Output output);
boolean deploy(String remoteRepositorySpec, Supplier<Collection<Artifact>> artifactSupplier, Output output)
throws Exception;

boolean deployAllRecorded(String remoteRepositorySpec, boolean stopRecording, Output output);
boolean deployAllRecorded(String remoteRepositorySpec, boolean stopRecording, Output output) throws Exception;

boolean install(Supplier<Collection<Artifact>> artifactSupplier, Output output);
boolean install(Supplier<Collection<Artifact>> artifactSupplier, Output output) throws Exception;

boolean listRepositories(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, Output output);
boolean listRepositories(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, Output output)
throws Exception;

boolean listAvailablePlugins(Collection<String> groupIds, Output output);
boolean listAvailablePlugins(Collection<String> groupIds, Output output) throws Exception;

boolean recordStart(Output output);

boolean recordStop(Output output);

boolean resolve(Collection<Artifact> artifacts, boolean sources, boolean javadoc, boolean signature, Output output);
boolean resolve(Collection<Artifact> artifacts, boolean sources, boolean javadoc, boolean signature, Output output)
throws Exception;

boolean resolveTransitive(
ResolutionScope resolutionScope,
Collection<ResolutionRoot> resolutionRoots,
boolean sources,
boolean javadoc,
boolean signature,
Output output);
Output output)
throws Exception;

boolean tree(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, boolean verbose, Output output);
boolean tree(ResolutionScope resolutionScope, ResolutionRoot resolutionRoot, boolean verbose, Output output)
throws Exception;

// Search API related commands: they target one single RemoteRepository

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ DependencyResult resolve(

Version findNewestVersion(Artifact artifact, boolean allowSnapshots) throws VersionRangeResolutionException;

List<Artifact> listAvailablePlugins(Collection<String> groupIds);
List<Artifact> listAvailablePlugins(Collection<String> groupIds) throws Exception;
}
Loading

0 comments on commit d5b3783

Please sign in to comment.