Skip to content

Commit 784eba8

Browse files
committed
Merge branch 'scijava/scijava-ops/improve-errors'
This PR improves the error messages resulting from Op matching, making it easier to tune Op requests. Current improvements focus in two areas * Identifying when two Ops conflict over a particular Op request. OpMatchingExceptions now describe all Ops that fully satisfy the request. * Clearly identifying when an Op was not provided due to the unavailability of an OpDependency. A new class, DependencyMatchingException, was created to chain the errors resulting from a failing dependency. DependencyMatchingException.getMessage() will return a String combining its own message with the messages of DependencyMatchingExceptions that are (super)causes of the returned exception. This helps users recognize that the problem does not lie with the requested Op, but instead lies with one of that Op's dependencies. An example message, describing an Op test.dependentOp that depends on test.missingDependencyOp, which in turn depends on test.nonexistingOp, is included below: See scijava/incubator#24
2 parents fe6fa6e + 2282bf9 commit 784eba8

3 files changed

Lines changed: 256 additions & 14 deletions

File tree

‎scijava/scijava-ops/src/main/java/org/scijava/ops/impl/DefaultOpEnvironment.java‎

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.scijava.ops.function.Computers.Arity1;
6969
import org.scijava.ops.matcher.DefaultOpMatcher;
7070
import org.scijava.ops.matcher.MatchingResult;
71+
import org.scijava.ops.matcher.DependencyMatchingException;
7172
import org.scijava.ops.matcher.MatchingUtils;
7273
import org.scijava.ops.matcher.OpAdaptationInfo;
7374
import org.scijava.ops.matcher.OpCandidate;
@@ -312,12 +313,16 @@ private OpCandidate findOpCandidate(OpRef ref, boolean adaptable, boolean simpli
312313
return findSimplifiedOp(ref);
313314
}
314315
catch (OpMatchingException e3) {
315-
// no direct, adapted, or simplified matches
316-
OpMatchingException matchingException = new OpMatchingException(
317-
"No Op available for request: " + ref, e1);
318-
matchingException.addSuppressed(e2);
319-
matchingException.addSuppressed(e3);
320-
throw matchingException;
316+
// NB: It is important that the adaptation and simplification errors be
317+
// suppressed here. If a failure occurs in Op matching, it
318+
// is not the fault of our adaptation/simplification process but is
319+
// instead due to the incongruity between the request and the set of
320+
// available Ops. Thus the error stemming from the direct match
321+
// attempt will provide the user with more information on how to fix
322+
// their Op request.
323+
e1.addSuppressed(e2);
324+
e1.addSuppressed(e3);
325+
throw e1;
321326
}
322327
}
323328
}
@@ -497,11 +502,14 @@ private List<Object> resolveOpDependencies(OpInfo info, Map<TypeVariable<?>, Typ
497502
final OpRef dependencyRef = inferOpRef(dependency, typeVarAssigns);
498503
try {
499504
resolvedDependencies.add(findOpInstance(dependencyRef, dependency.isAdaptable(), false));
500-
} catch (final Exception e) {
501-
throw new OpMatchingException("Could not find Op that matches requested Op dependency:" + "\nOp class: "
502-
+ info.implementationName() + //
503-
"\nDependency identifier: " + dependency.getKey() + //
504-
"\n\n Attempted request:\n" + dependencyRef, e);
505+
}
506+
catch (final OpMatchingException e) {
507+
String message = DependencyMatchingException.message(info
508+
.implementationName(), dependency.getKey(), dependencyRef);
509+
if (e instanceof DependencyMatchingException) {
510+
throw new DependencyMatchingException(message, (DependencyMatchingException) e);
511+
}
512+
throw new DependencyMatchingException(message);
505513
}
506514
}
507515
return resolvedDependencies;
@@ -528,6 +536,7 @@ private List<Object> resolveOpDependencies(OpInfo info, Map<TypeVariable<?>, Typ
528536
*/
529537
private OpCandidate adaptOp(OpRef ref) throws OpMatchingException {
530538

539+
List<DependencyMatchingException> depExceptions = new ArrayList<>();
531540
for (final OpInfo adaptor : infos("adapt")) {
532541
Type adaptTo = adaptor.output().getType();
533542
Map<TypeVariable<?>, Type> map = new HashMap<>();
@@ -568,14 +577,24 @@ private OpCandidate adaptOp(OpRef ref) throws OpMatchingException {
568577
adaptedCandidate.setStatus(StatusCode.MATCH);
569578
return adaptedCandidate;
570579
}
580+
catch (DependencyMatchingException d) {
581+
depExceptions.add(d);
582+
}
571583
catch (OpMatchingException e1) {
572584
log.trace(e1);
573585
}
574586
}
575587

576-
// no adaptors available.
577-
throw new OpMatchingException(
578-
"Op adaptation failed: no adaptable Ops of type " + ref.getName());
588+
//no adaptors available.
589+
if (depExceptions.size() == 0) {
590+
throw new OpMatchingException(
591+
"Op adaptation failed: no adaptable Ops of type " + ref.getName());
592+
}
593+
StringBuilder sb = new StringBuilder();
594+
for (DependencyMatchingException d : depExceptions) {
595+
sb.append("\n\n" + d.getMessage());
596+
}
597+
throw new DependencyMatchingException(sb.toString());
579598
}
580599

581600
private boolean adaptOpOutputSatisfiesRefTypes(Type adaptTo, Map<TypeVariable<?>, Type> map, OpRef ref) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
package org.scijava.ops.matcher;
3+
4+
/**
5+
* An {@link OpMatchingException} caused by another {@code OpMatchingException}
6+
* thrown when resolving dependencies. We illustrate the need for
7+
* {@code DependencyMatchingException} with an example: Suppose Op {@code A} has
8+
* an dependency on Op {@code B}, which in turn has a dependency on Op
9+
* {@code C}. Suppose further that {@code A} and {@code B} are found, but no
10+
* {@code C} is found to satisfy {@code B}. This will result in an
11+
* {@link OpMatchingException} thrown for Op {@code A}. We would want to know:
12+
* <ol>
13+
* <li>that the request for {@code A} was not fulfilled
14+
* <li>that no match could be found for {@code C}, which was the cause for (1)
15+
* </ol>
16+
* This logic can be generalized for an arbitrarily long Op chain.
17+
*
18+
* TODO: Consider supporting non-DependencyMatchingException {@code cause}s.
19+
*
20+
* @author Gabriel Selzer
21+
*/
22+
public class DependencyMatchingException extends OpMatchingException {
23+
24+
public DependencyMatchingException(final String message) {
25+
super(message);
26+
}
27+
28+
public static String message(String dependentOp, final String dependencyName, OpRef dependency) {
29+
return "Could not instantiate dependency of: " + dependentOp +
30+
"\nDependency identifier: " + dependencyName + //
31+
"\n\nAttempted request:\n" + dependency;
32+
}
33+
34+
public DependencyMatchingException(final String message,
35+
final DependencyMatchingException cause)
36+
{
37+
super(message, cause);
38+
}
39+
40+
@Override
41+
public String getMessage() {
42+
if (getCause() == null) return super.getMessage();
43+
return super.getMessage() + "\n\nCause:\n\n" +
44+
getCause().getMessage();
45+
}
46+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
2+
package org.scijava.ops.matcher;
3+
4+
import java.util.function.Function;
5+
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
import org.scijava.ops.function.Computers;
9+
import org.scijava.ops.AbstractTestEnvironment;
10+
import org.scijava.ops.OpDependency;
11+
import org.scijava.ops.OpField;
12+
import org.scijava.ops.core.Op;
13+
import org.scijava.ops.core.OpCollection;
14+
import org.scijava.param.Parameter;
15+
import org.scijava.plugin.Plugin;
16+
import org.scijava.struct.ItemIO;
17+
18+
@Plugin(type = OpCollection.class)
19+
public class DefaultMatchingErrorTest extends AbstractTestEnvironment {
20+
21+
@OpField(names = "test.duplicateOp")
22+
public final Function<Double, Double> duplicateA = (in) -> in;
23+
24+
@OpField(names = "test.duplicateOp")
25+
public final Function<Double, Double> duplicateB = (in) -> in;
26+
27+
/**
28+
* Assert a relevant (i.e. informational) message is thrown when multiple Ops
29+
* conflict for a given OpRef.
30+
*/
31+
@Test
32+
public void duplicateErrorRegressionTest() {
33+
try {
34+
ops.op("test.duplicateOp").inType(Double.class).outType(Double.class)
35+
.function();
36+
Assert.fail();
37+
}
38+
catch (IllegalArgumentException e) {
39+
Assert.assertTrue(e.getMessage().startsWith(
40+
"org.scijava.ops.matcher.OpMatchingException: " +
41+
"Multiple 'test.duplicateOp/java.util.function.Function<java.lang.Double, java.lang.Double>' " +
42+
"ops of priority 0.0:"));
43+
}
44+
}
45+
46+
/**
47+
* Assert that a relevant error is thrown when a dependency is missing
48+
*/
49+
@Test
50+
public void missingDependencyRegressionTest() {
51+
try {
52+
ops.op("test.missingDependencyOp").input(1.).outType(Double.class)
53+
.apply();
54+
Assert.fail("Expected DependencyMatchingException");
55+
}
56+
catch (IllegalArgumentException e) {
57+
Throwable cause = e.getCause();
58+
Assert.assertTrue(cause instanceof DependencyMatchingException);
59+
String message = cause.getMessage();
60+
Assert.assertTrue(message.contains("Name: \"test.nonexistingOp\""));
61+
}
62+
}
63+
64+
/**
65+
* Assert the DependencyMatchingException contains both dependencies in the
66+
* chain
67+
*/
68+
@Test
69+
public void missingNestedDependencyRegressionTest() {
70+
try {
71+
ops.op("test.outsideOp").input(1.).outType(Double.class).apply();
72+
Assert.fail("Expected DependencyMatchingException");
73+
}
74+
catch (IllegalArgumentException e) {
75+
Throwable cause = e.getCause();
76+
Assert.assertTrue(cause instanceof DependencyMatchingException);
77+
String message = cause.getMessage();
78+
Assert.assertTrue(message.contains("Name: \"test.missingDependencyOp\""));
79+
Assert.assertTrue(message.contains("Name: \"test.nonexistingOp\""));
80+
}
81+
}
82+
83+
/**
84+
* Assert that DependencyMatchingExceptions do not omit relevant information
85+
* when an adaptation is involved.
86+
*/
87+
@Test
88+
public void missingDependencyViaAdaptationTest() {
89+
Double[] d = new Double[0];
90+
try {
91+
ops.op("test.adaptMissingDep").input(d).outType(Double[].class).apply();
92+
Assert.fail("Expected DependencyMatchingException");
93+
}
94+
catch (IllegalArgumentException e) {
95+
Throwable cause = e.getCause();
96+
Assert.assertTrue(cause instanceof DependencyMatchingException);
97+
String message = cause.getMessage();
98+
Assert.assertTrue(message.contains("adapted"));
99+
Assert.assertTrue(message.contains("Name: \"test.nonexistingOp\""));
100+
101+
}
102+
}
103+
104+
}
105+
106+
@Plugin(type = Op.class, name = "test.furtherOutsideOp")
107+
@Parameter(key = "in")
108+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
109+
class FurtherDependentOp implements Function<Double, Double> {
110+
111+
@OpDependency(name = "test.outsideOp")
112+
private Function<Double, Double> op;
113+
114+
@Override
115+
public Double apply(Double t) {
116+
return op.apply(t);
117+
}
118+
119+
}
120+
121+
@Plugin(type = Op.class, name = "test.outsideOp")
122+
@Parameter(key = "in")
123+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
124+
class DependentOp implements Function<Double, Double> {
125+
126+
@OpDependency(name = "test.missingDependencyOp")
127+
private Function<Double, Double> op;
128+
129+
@Override
130+
public Double apply(Double t) {
131+
return op.apply(t);
132+
}
133+
134+
}
135+
136+
@Plugin(type = Op.class, name = "test.missingDependencyOp")
137+
@Parameter(key = "in")
138+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
139+
class MissingDependencyOp implements Function<Double, Double> {
140+
141+
@OpDependency(name = "test.nonexistingOp")
142+
private Function<Double, Double> op;
143+
144+
@Override
145+
public Double apply(Double t) {
146+
return op.apply(t);
147+
}
148+
149+
}
150+
151+
@Plugin(type = Op.class, name = "test.adaptMissingDep")
152+
@Parameter(key = "in")
153+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
154+
class MissingDependencyOpArr1 implements Computers.Arity1<Double[], Double[]> {
155+
156+
@OpDependency(name = "test.nonexistingOp")
157+
private Function<Double, Double> op;
158+
159+
@Override
160+
public void compute(Double[] t, Double[] out) {}
161+
162+
}
163+
164+
@Plugin(type = Op.class, name = "test.adaptMissingDep")
165+
@Parameter(key = "in")
166+
@Parameter(key = "out", itemIO = ItemIO.OUTPUT)
167+
class MissingDependencyOpArr2 implements Function<Double, Double> {
168+
169+
@OpDependency(name = "test.nonexistingOp")
170+
private Function<Double, Double> op;
171+
172+
@Override
173+
public Double apply(Double t) {
174+
return op.apply(t);
175+
}
176+
177+
}

0 commit comments

Comments
 (0)