Skip to content

Commit

Permalink
Expose errors to the host
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi committed Nov 8, 2024
1 parent e392fe5 commit 5571ad1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/extism/chicory/sdk/ExtismException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.extism.chicory.sdk;

public class ExtismException extends RuntimeException{
public class ExtismException extends RuntimeException {

public ExtismException(String message) {
super(message);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/extism/chicory/sdk/ExtismFunctionException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.extism.chicory.sdk;

public class ExtismFunctionException extends ExtismException {

private final String error;

public ExtismFunctionException(String function, String message) {
super(String.format("function %s returned an error: %s", function, message));
this.error = message;
}

/**
* Underlying error returned by the plugin call
*/
public String getError() {
return error;
}

}
5 changes: 5 additions & 0 deletions src/main/java/org/extism/chicory/sdk/HostEnv.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ public byte[] getOutput() {
return kernel.getOutput();
}

public String getError() {
return kernel.getError();
}

public Memory memory() {
return this.memory;
}


public class Memory {

public long length(long offset) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/extism/chicory/sdk/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import com.dylibso.chicory.runtime.HostFunction;
import com.dylibso.chicory.runtime.Instance;
import com.dylibso.chicory.wasm.Parser;
import com.dylibso.chicory.wasm.types.Value;
import com.dylibso.chicory.wasm.types.ValueType;

import java.util.List;

import static com.dylibso.chicory.wasm.types.Value.i64;

public class Kernel {
static final String IMPORT_MODULE_NAME = "extism:host/env";
final com.dylibso.chicory.runtime.Memory instanceMemory;
Expand Down Expand Up @@ -77,6 +74,11 @@ byte[] getOutput() {
return instanceMemory.readBytes((int) ptr, (int) len);
}

public String getError() {
long ptr = errorGet.apply()[0];
long len = length.apply(ptr)[0];
return instanceMemory.readString((int) ptr, (int) len);
}

HostFunction[] toHostFunctions() {
var hostFunctions = new HostFunction[20];
Expand Down Expand Up @@ -241,7 +243,7 @@ HostFunction[] toHostFunctions() {
"error_set",
List.of(ValueType.I64),
List.of(),
(Instance instance, long... args) -> reset.apply(args)
(Instance instance, long... args) -> errorSet.apply(args)
);

hostFunctions[count++] =
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/extism/chicory/sdk/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public byte[] call(String funcName, byte[] input) {
if (result == 0) {
return hostEnv.getOutput();
} else {
throw new ExtismException("Failed");
String error = hostEnv.getError();
throw new ExtismFunctionException(funcName, error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.dylibso.chicory.wasm.types.Value;
import junit.framework.TestCase;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ExtismHostFunctionTest extends TestCase {
Expand Down

0 comments on commit 5571ad1

Please sign in to comment.