Skip to content

Commit

Permalink
[api] improve error message
Browse files Browse the repository at this point in the history
Change-Id: I6032a92bc119c1db98eeebe8b088a3c95ea5084d
  • Loading branch information
frankfliu committed Nov 7, 2021
1 parent 551ad00 commit fcb5a7f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public Output processOutput(TranslatorContext ctx, NDList list) throws Exception
public NDList processInput(TranslatorContext ctx, Input input) throws Exception {
BytesSupplier data = input.getData();
try {
if (data == null) {
throw new TranslateException("Input data is empty.");
}
Image image = factory.fromInputStream(new ByteArrayInputStream(data.getAsBytes()));
return translator.processInput(ctx, image);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import ai.djl.ndarray.BytesSupplier;
import ai.djl.ndarray.NDList;
import ai.djl.translate.Batchifier;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import ai.djl.translate.TranslatorContext;
import ai.djl.util.JsonUtils;
import ai.djl.util.PairList;
import com.google.gson.JsonParseException;

/**
* A {@link Translator} that can handle generic question answering {@link Input} and {@link Output}.
Expand Down Expand Up @@ -61,7 +63,15 @@ public NDList processInput(TranslatorContext ctx, Input input) throws Exception
String paragraph = input.getAsString("paragraph");
qa = new QAInput(question, paragraph);
} else {
qa = JsonUtils.GSON.fromJson(input.getData().getAsString(), QAInput.class);
BytesSupplier data = input.getData();
if (data == null) {
throw new TranslateException("Input data is empty.");
}
try {
qa = JsonUtils.GSON.fromJson(data.getAsString(), QAInput.class);
} catch (JsonParseException e) {
throw new TranslateException("Input is not a valid json.");
}
}
return translator.processInput(ctx, qa);
}
Expand Down

0 comments on commit fcb5a7f

Please sign in to comment.