Add version of resolved Pkl distribution to ExecutorException (#719)

* Add version of resolved Pkl distribution to `ExecutorException`

* Review comments

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>

---------

Co-authored-by: Daniel Chao <daniel.h.chao@gmail.com>
This commit is contained in:
Philip K.F. Hölzenspies
2024-10-23 10:51:53 +01:00
committed by GitHub
parent a7cc098925
commit 0aa4819cea
2 changed files with 30 additions and 1 deletions

View File

@@ -226,7 +226,7 @@ final class EmbeddedExecutor implements Executor {
try {
return executorSpi.evaluatePath(modulePath, options.toSpiOptions());
} catch (ExecutorSpiException e) {
throw new ExecutorException(e.getMessage(), e.getCause());
throw new ExecutorException(e.getMessage(), e.getCause(), executorSpi.getPklVersion());
} finally {
currentThread.setContextClassLoader(prevContextClassLoader);
}

View File

@@ -15,15 +15,44 @@
*/
package org.pkl.executor;
import java.util.Objects;
/**
* Indicates an {@link Executor} error. {@link #getMessage()} returns a user-facing error message.
*/
public final class ExecutorException extends RuntimeException {
private final String pklVersion;
public ExecutorException(String message) {
super(message);
pklVersion = null;
}
public ExecutorException(String message, Throwable cause) {
super(message, cause);
pklVersion = null;
}
public ExecutorException(String message, Throwable cause, String version) {
super(message, cause);
pklVersion = Objects.requireNonNull(version);
}
/**
* The selected Pkl version used to evaluate the module.
*
* <p>Returns {@code null} if this exception does not originate from an underlying Pkl evaluator.
*/
public String getPklVersion() {
return pklVersion;
}
@Override
public String getMessage() {
var message = super.getMessage();
if (pklVersion == null) {
return message;
}
return message + "\nPkl version: " + pklVersion;
}
}