Skip to content

Commit

Permalink
Create an exception type for a failed serialization / deserialization…
Browse files Browse the repository at this point in the history
… in the ProtobufAnnotationSerializer
  • Loading branch information
AngledLuffa committed Oct 14, 2023
1 parent ed6433e commit 29f2f26
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ public static class LossySerializationException extends RuntimeException {
private LossySerializationException(String msg) { super(msg); }
}

public static class FailedSerializationError extends RuntimeException {
private static final long serialVersionUID = 8142679843568354709L;

private FailedSerializationError(String msg) { super(msg); }
}

/**
* If true, serialization is guaranteed to be lossless or else a runtime exception is thrown
* at serialization time.
Expand Down Expand Up @@ -2427,7 +2433,7 @@ private static SemanticGraph fromProto(CoreNLPProtos.DependencyGraph proto, List
} else {
token = originalLabels.get(in.getIndex(), in.getEmptyIndex());
if (token == null) {
throw new NullPointerException("Could not find the token for index " + in.getIndex());
throw new FailedSerializationError("Could not find the token for index " + in.getIndex());
}
}
IndexedWord word;
Expand Down Expand Up @@ -2459,18 +2465,17 @@ private static SemanticGraph fromProto(CoreNLPProtos.DependencyGraph proto, List
for(CoreNLPProtos.DependencyGraph.Edge ie: proto.getEdgeList()){
IndexedWord source = nodes.get(ie.getSource(), ie.getSourceEmpty(), ie.getSourceCopy());
if (source == null) {
throw new AssertionError("Source of a dependency was null! Edge: " + ie);
throw new FailedSerializationError("Source of a dependency was null!\nEdge: " + ie);
}
IndexedWord target = nodes.get(ie.getTarget(), ie.getTargetEmpty(), ie.getTargetCopy());
if (target == null) {
throw new AssertionError("Target of a dependency was null! Edge: " + ie);
throw new FailedSerializationError("Target of a dependency was null!\nEdge: " + ie);
}
synchronized (globalLock) {
// this is not thread-safe: there are static fields in GrammaticalRelation
if (!ie.hasDep()) {
throw new AssertionError("Protobuf dependency edge was null! Edge: " + ie);
throw new FailedSerializationError("Protobuf dependency edge was null!\nEdge: " + ie);
}
assert ie.hasDep();
GrammaticalRelation rel = GrammaticalRelation.valueOf(fromProto(ie.getLanguage()), ie.getDep());
graph.addEdge(source, target, rel, 1.0, ie.hasIsExtra() && ie.getIsExtra());
}
Expand Down

0 comments on commit 29f2f26

Please sign in to comment.