From 320165ef623c9960763ae914e9bab748a5363ef9 Mon Sep 17 00:00:00 2001 From: John Bauer Date: Fri, 13 Oct 2023 21:53:26 -0700 Subject: [PATCH] Create an exception type for a failed serialization / deserialization in the ProtobufAnnotationSerializer --- .../pipeline/ProtobufAnnotationSerializer.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java b/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java index b8238f3ad6..735ecf4b59 100644 --- a/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java +++ b/src/edu/stanford/nlp/pipeline/ProtobufAnnotationSerializer.java @@ -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. @@ -2417,7 +2423,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; @@ -2449,18 +2455,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()); }