Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit ef48449

Browse files
authored
Merge pull request #759 from sebright/deserialization-exception
Rename TagContextParseException to TagContextDeserializationException.
2 parents d1e5e9f + 38c7a2d commit ef48449

File tree

8 files changed

+53
-49
lines changed

8 files changed

+53
-49
lines changed

api/src/main/java/io/opencensus/tags/propagation/TagContextBinarySerializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public abstract class TagContextBinarySerializer {
4545
*
4646
* @param bytes on-the-wire representation of a {@code TagContext}.
4747
* @return a {@code TagContext} deserialized from {@code bytes}.
48-
* @throws TagContextParseException if there is a parse error or the serialized {@code TagContext}
49-
* contains invalid tags.
48+
* @throws TagContextDeserializationException if there is a parse error, the input contains
49+
* invalid tags, or the input is larger than 8192 bytes.
5050
*/
51-
public abstract TagContext fromByteArray(byte[] bytes) throws TagContextParseException;
51+
public abstract TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException;
5252
}

api/src/main/java/io/opencensus/tags/propagation/TagContextParseException.java renamed to api/src/main/java/io/opencensus/tags/propagation/TagContextDeserializationException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@
1919
import io.opencensus.tags.TagContext;
2020

2121
/** Exception thrown when a {@link TagContext} cannot be parsed. */
22-
public final class TagContextParseException extends Exception {
22+
public final class TagContextDeserializationException extends Exception {
2323
private static final long serialVersionUID = 0L;
2424

2525
/**
2626
* Constructs a new {@code TagContextParseException} with the given message.
2727
*
28-
* @param message a message describing the parse error.
28+
* @param message a message describing the error.
2929
*/
30-
public TagContextParseException(String message) {
30+
public TagContextDeserializationException(String message) {
3131
super(message);
3232
}
3333

3434
/**
3535
* Constructs a new {@code TagContextParseException} with the given message and cause.
3636
*
37-
* @param message a message describing the parse error.
38-
* @param cause the cause of the parse error.
37+
* @param message a message describing the error.
38+
* @param cause the cause of the error.
3939
*/
40-
public TagContextParseException(String message, Throwable cause) {
40+
public TagContextDeserializationException(String message, Throwable cause) {
4141
super(message, cause);
4242
}
4343
}

api/src/test/java/io/opencensus/tags/NoopTagsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.google.common.collect.Lists;
2222
import io.opencensus.internal.NoopScope;
2323
import io.opencensus.tags.propagation.TagContextBinarySerializer;
24-
import io.opencensus.tags.propagation.TagContextParseException;
24+
import io.opencensus.tags.propagation.TagContextDeserializationException;
2525
import io.opencensus.tags.propagation.TagContextSerializationException;
2626
import java.util.Arrays;
2727
import java.util.Iterator;
@@ -131,7 +131,7 @@ public void noopTagPropagationComponent() {
131131

132132
@Test
133133
public void noopTagContextBinarySerializer()
134-
throws TagContextParseException, TagContextSerializationException {
134+
throws TagContextDeserializationException, TagContextSerializationException {
135135
assertThat(NoopTags.getNoopTagContextBinarySerializer().toByteArray(TAG_CONTEXT))
136136
.isEqualTo(new byte[0]);
137137
assertThat(NoopTags.getNoopTagContextBinarySerializer().fromByteArray(new byte[5]))
@@ -148,7 +148,7 @@ public void noopTagContextBinarySerializer_ToByteArray_DisallowsNull()
148148

149149
@Test
150150
public void noopTagContextBinarySerializer_FromByteArray_DisallowsNull()
151-
throws TagContextParseException {
151+
throws TagContextDeserializationException {
152152
TagContextBinarySerializer noopSerializer = NoopTags.getNoopTagContextBinarySerializer();
153153
thrown.expect(NullPointerException.class);
154154
noopSerializer.fromByteArray(null);

api/src/test/java/io/opencensus/tags/propagation/TagContextParseExceptionTest.java renamed to api/src/test/java/io/opencensus/tags/propagation/TagContextDeserializationExceptionTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@
2323
import org.junit.runner.RunWith;
2424
import org.junit.runners.JUnit4;
2525

26-
/** Unit tests for {@link TagContextParseException}. */
26+
/** Unit tests for {@link TagContextDeserializationException}. */
2727
@RunWith(JUnit4.class)
28-
public final class TagContextParseExceptionTest {
28+
public final class TagContextDeserializationExceptionTest {
2929

3030
@Test
3131
public void createWithMessage() {
32-
assertThat(new TagContextParseException("my message").getMessage()).isEqualTo("my message");
32+
assertThat(new TagContextDeserializationException("my message").getMessage())
33+
.isEqualTo("my message");
3334
}
3435

3536
@Test
3637
public void createWithMessageAndCause() {
3738
IOException cause = new IOException();
38-
TagContextParseException parseException = new TagContextParseException("my message", cause);
39-
assertThat(parseException.getMessage()).isEqualTo("my message");
40-
assertThat(parseException.getCause()).isEqualTo(cause);
39+
TagContextDeserializationException exception =
40+
new TagContextDeserializationException("my message", cause);
41+
assertThat(exception.getMessage()).isEqualTo("my message");
42+
assertThat(exception.getCause()).isEqualTo(cause);
4143
}
4244
}

core_impl/src/main/java/io/opencensus/implcore/tags/propagation/SerializationUtils.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import io.opencensus.tags.TagContext;
2828
import io.opencensus.tags.TagKey;
2929
import io.opencensus.tags.TagValue;
30-
import io.opencensus.tags.propagation.TagContextParseException;
30+
import io.opencensus.tags.propagation.TagContextDeserializationException;
3131
import java.nio.BufferUnderflowException;
3232
import java.nio.ByteBuffer;
3333
import java.util.HashMap;
@@ -83,27 +83,27 @@ static byte[] serializeBinary(TagContext tags) {
8383

8484
// Deserializes input to TagContext based on the binary format standard.
8585
// The encoded tags are of the form: <version_id><encoded_tags>
86-
static TagContextImpl deserializeBinary(byte[] bytes) throws TagContextParseException {
86+
static TagContextImpl deserializeBinary(byte[] bytes) throws TagContextDeserializationException {
8787
try {
8888
if (bytes.length == 0) {
8989
// Does not allow empty byte array.
90-
throw new TagContextParseException("Input byte[] can not be empty.");
90+
throw new TagContextDeserializationException("Input byte[] can not be empty.");
9191
}
9292

9393
ByteBuffer buffer = ByteBuffer.wrap(bytes).asReadOnlyBuffer();
9494
int versionId = buffer.get();
9595
if (versionId != VERSION_ID) {
96-
throw new TagContextParseException(
96+
throw new TagContextDeserializationException(
9797
"Wrong Version ID: " + versionId + ". Currently supported version is: " + VERSION_ID);
9898
}
9999
return new TagContextImpl(parseTags(buffer));
100100
} catch (BufferUnderflowException exn) {
101-
throw new TagContextParseException(exn.toString()); // byte array format error.
101+
throw new TagContextDeserializationException(exn.toString()); // byte array format error.
102102
}
103103
}
104104

105105
private static Map<TagKey, TagValue> parseTags(ByteBuffer buffer)
106-
throws TagContextParseException {
106+
throws TagContextDeserializationException {
107107
Map<TagKey, TagValue> tags = new HashMap<TagKey, TagValue>();
108108
int limit = buffer.limit();
109109
while (buffer.position() < limit) {
@@ -123,22 +123,23 @@ private static Map<TagKey, TagValue> parseTags(ByteBuffer buffer)
123123

124124
// TODO(sebright): Consider exposing a TagKey name validation method to avoid needing to catch an
125125
// IllegalArgumentException here.
126-
private static final TagKey createTagKey(String name) throws TagContextParseException {
126+
private static final TagKey createTagKey(String name) throws TagContextDeserializationException {
127127
try {
128128
return TagKey.create(name);
129129
} catch (IllegalArgumentException e) {
130-
throw new TagContextParseException("Invalid tag key: " + name, e);
130+
throw new TagContextDeserializationException("Invalid tag key: " + name, e);
131131
}
132132
}
133133

134134
// TODO(sebright): Consider exposing a TagValue validation method to avoid needing to catch
135135
// an IllegalArgumentException here.
136136
private static final TagValue createTagValue(TagKey key, String value)
137-
throws TagContextParseException {
137+
throws TagContextDeserializationException {
138138
try {
139139
return TagValue.create(value);
140140
} catch (IllegalArgumentException e) {
141-
throw new TagContextParseException("Invalid tag value for key " + key + ": " + value, e);
141+
throw new TagContextDeserializationException(
142+
"Invalid tag value for key " + key + ": " + value, e);
142143
}
143144
}
144145

core_impl/src/main/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import io.opencensus.tags.TagContext;
2222
import io.opencensus.tags.TaggingState;
2323
import io.opencensus.tags.propagation.TagContextBinarySerializer;
24-
import io.opencensus.tags.propagation.TagContextParseException;
24+
import io.opencensus.tags.propagation.TagContextDeserializationException;
2525

2626
final class TagContextBinarySerializerImpl extends TagContextBinarySerializer {
2727
private static final byte[] EMPTY_BYTE_ARRAY = {};
@@ -40,7 +40,7 @@ public byte[] toByteArray(TagContext tags) {
4040
}
4141

4242
@Override
43-
public TagContext fromByteArray(byte[] bytes) throws TagContextParseException {
43+
public TagContext fromByteArray(byte[] bytes) throws TagContextDeserializationException {
4444
return state.get() == TaggingState.DISABLED
4545
? TagContextImpl.EMPTY
4646
: SerializationUtils.deserializeBinary(bytes);

core_impl/src/test/java/io/opencensus/implcore/tags/propagation/TagContextBinarySerializerImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import io.opencensus.tags.TaggingState;
2929
import io.opencensus.tags.TagsComponent;
3030
import io.opencensus.tags.propagation.TagContextBinarySerializer;
31-
import io.opencensus.tags.propagation.TagContextParseException;
31+
import io.opencensus.tags.propagation.TagContextDeserializationException;
3232
import io.opencensus.tags.propagation.TagContextSerializationException;
3333
import java.util.Iterator;
3434
import org.junit.Test;
@@ -73,15 +73,15 @@ public void toByteArray_TaggingReenabled() throws TagContextSerializationExcepti
7373

7474
@Test
7575
public void fromByteArray_TaggingDisabled()
76-
throws TagContextParseException, TagContextSerializationException {
76+
throws TagContextDeserializationException, TagContextSerializationException {
7777
byte[] serialized = serializer.toByteArray(tagContext);
7878
tagsComponent.setState(TaggingState.DISABLED);
7979
assertThat(TagsTestUtil.tagContextToList(serializer.fromByteArray(serialized))).isEmpty();
8080
}
8181

8282
@Test
8383
public void fromByteArray_TaggingReenabled()
84-
throws TagContextParseException, TagContextSerializationException {
84+
throws TagContextDeserializationException, TagContextSerializationException {
8585
byte[] serialized = serializer.toByteArray(tagContext);
8686
tagsComponent.setState(TaggingState.DISABLED);
8787
assertThat(TagsTestUtil.tagContextToList(serializer.fromByteArray(serialized))).isEmpty();

core_impl/src/test/java/io/opencensus/implcore/tags/propagation/TagContextDeserializationTest.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import io.opencensus.tags.Tagger;
3030
import io.opencensus.tags.TagsComponent;
3131
import io.opencensus.tags.propagation.TagContextBinarySerializer;
32-
import io.opencensus.tags.propagation.TagContextParseException;
32+
import io.opencensus.tags.propagation.TagContextDeserializationException;
3333
import org.junit.Rule;
3434
import org.junit.Test;
3535
import org.junit.rules.ExpectedException;
@@ -58,7 +58,7 @@ public void testVersionAndValueTypeConstants() {
5858
}
5959

6060
@Test
61-
public void testDeserializeNoTags() throws TagContextParseException {
61+
public void testDeserializeNoTags() throws TagContextDeserializationException {
6262
TagContext expected = tagger.empty();
6363
TagContext actual =
6464
serializer.fromByteArray(
@@ -67,42 +67,43 @@ public void testDeserializeNoTags() throws TagContextParseException {
6767
}
6868

6969
@Test
70-
public void testDeserializeEmptyByteArrayThrowException() throws TagContextParseException {
71-
thrown.expect(TagContextParseException.class);
70+
public void testDeserializeEmptyByteArrayThrowException()
71+
throws TagContextDeserializationException {
72+
thrown.expect(TagContextDeserializationException.class);
7273
thrown.expectMessage("Input byte[] can not be empty.");
7374
serializer.fromByteArray(new byte[0]);
7475
}
7576

7677
@Test
77-
public void testDeserializeInvalidTagKey() throws TagContextParseException {
78+
public void testDeserializeInvalidTagKey() throws TagContextDeserializationException {
7879
ByteArrayDataOutput output = ByteStreams.newDataOutput();
7980
output.write(SerializationUtils.VERSION_ID);
8081

8182
// Encode an invalid tag key and a valid tag value:
8283
encodeTagToOutput("\2key", "value", output);
8384
final byte[] bytes = output.toByteArray();
8485

85-
thrown.expect(TagContextParseException.class);
86+
thrown.expect(TagContextDeserializationException.class);
8687
thrown.expectMessage("Invalid tag key: \2key");
8788
serializer.fromByteArray(bytes);
8889
}
8990

9091
@Test
91-
public void testDeserializeInvalidTagValue() throws TagContextParseException {
92+
public void testDeserializeInvalidTagValue() throws TagContextDeserializationException {
9293
ByteArrayDataOutput output = ByteStreams.newDataOutput();
9394
output.write(SerializationUtils.VERSION_ID);
9495

9596
// Encode a valid tag key and an invalid tag value:
9697
encodeTagToOutput("my key", "val\3", output);
9798
final byte[] bytes = output.toByteArray();
9899

99-
thrown.expect(TagContextParseException.class);
100+
thrown.expect(TagContextDeserializationException.class);
100101
thrown.expectMessage("Invalid tag value for key TagKey{name=my key}: val\3");
101102
serializer.fromByteArray(bytes);
102103
}
103104

104105
@Test
105-
public void testDeserializeOneTag() throws TagContextParseException {
106+
public void testDeserializeOneTag() throws TagContextDeserializationException {
106107
ByteArrayDataOutput output = ByteStreams.newDataOutput();
107108
output.write(SerializationUtils.VERSION_ID);
108109
encodeTagToOutput("Key", "Value", output);
@@ -115,7 +116,7 @@ public void testDeserializeOneTag() throws TagContextParseException {
115116
}
116117

117118
@Test
118-
public void testDeserializeMultipleTags() throws TagContextParseException {
119+
public void testDeserializeMultipleTags() throws TagContextDeserializationException {
119120
ByteArrayDataOutput output = ByteStreams.newDataOutput();
120121
output.write(SerializationUtils.VERSION_ID);
121122
encodeTagToOutput("Key1", "Value1", output);
@@ -130,7 +131,7 @@ public void testDeserializeMultipleTags() throws TagContextParseException {
130131
}
131132

132133
@Test
133-
public void stopParsingAtUnknownField() throws TagContextParseException {
134+
public void stopParsingAtUnknownField() throws TagContextDeserializationException {
134135
ByteArrayDataOutput output = ByteStreams.newDataOutput();
135136
output.write(SerializationUtils.VERSION_ID);
136137
encodeTagToOutput("Key1", "Value1", output);
@@ -153,7 +154,7 @@ public void stopParsingAtUnknownField() throws TagContextParseException {
153154
}
154155

155156
@Test
156-
public void stopParsingAtUnknownTagAtStart() throws TagContextParseException {
157+
public void stopParsingAtUnknownTagAtStart() throws TagContextDeserializationException {
157158
ByteArrayDataOutput output = ByteStreams.newDataOutput();
158159
output.write(SerializationUtils.VERSION_ID);
159160

@@ -166,15 +167,15 @@ public void stopParsingAtUnknownTagAtStart() throws TagContextParseException {
166167
}
167168

168169
@Test
169-
public void testDeserializeWrongFormat() throws TagContextParseException {
170+
public void testDeserializeWrongFormat() throws TagContextDeserializationException {
170171
// encoded tags should follow the format <version_id>(<tag_field_id><tag_encoding>)*
171-
thrown.expect(TagContextParseException.class);
172+
thrown.expect(TagContextDeserializationException.class);
172173
serializer.fromByteArray(new byte[3]);
173174
}
174175

175176
@Test
176-
public void testDeserializeWrongVersionId() throws TagContextParseException {
177-
thrown.expect(TagContextParseException.class);
177+
public void testDeserializeWrongVersionId() throws TagContextDeserializationException {
178+
thrown.expect(TagContextDeserializationException.class);
178179
thrown.expectMessage("Wrong Version ID: 1. Currently supported version is: 0");
179180
serializer.fromByteArray(new byte[] {(byte) (SerializationUtils.VERSION_ID + 1)});
180181
}

0 commit comments

Comments
 (0)