Skip to content

Commit

Permalink
DynamicCodec emits a trail of type names when it encounters NoSuchCod…
Browse files Browse the repository at this point in the history
…ecException.

This will make it easier to trace down missing codecs.

PiperOrigin-RevId: 191920743
  • Loading branch information
aoeui authored and Copybara-Service committed Apr 6, 2018
1 parent 4282c19 commit e4c64ac
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ private void serializeField(
serializeField(context, codedOut, arr, type.getComponentType(), base + scale * i);
}
} else {
context.serialize(UnsafeProvider.getInstance().getObject(obj, offset), codedOut);
try {
context.serialize(UnsafeProvider.getInstance().getObject(obj, offset), codedOut);
} catch (SerializationException.NoCodecException e) {
e.addTrail(this.type);
throw e;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.devtools.build.lib.skyframe.serialization;

import java.io.NotSerializableException;
import java.util.ArrayList;

/** Exception signaling a failure to Serialize or Deserialize an Object. */
public class SerializationException extends Exception {
Expand All @@ -35,6 +36,8 @@ public SerializationException(String msg, Throwable cause) {
* or type of object.
*/
public static class NoCodecException extends SerializationException {
ArrayList<String> trail = new ArrayList<>();

NoCodecException(String message) {
super(message);
}
Expand All @@ -51,6 +54,20 @@ public static class NoCodecException extends SerializationException {
NoCodecException(String message, NoCodecException e) {
super(message, e);
}

@Override
public String getMessage() {
return super.getMessage() + (trail.isEmpty() ? "" : " " + trail);
}

/**
* Adds extra tracing info for debugging.
*
* <p>Primarily useful for {@link DynamicCodec}.
*/
public void addTrail(Class<?> type) {
trail.add(type.getName());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package com.google.devtools.build.lib.skyframe.serialization;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.skyframe.serialization.testutils.SerializationTester;
import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.Objects;
import org.junit.Test;
Expand Down Expand Up @@ -371,4 +374,32 @@ public void testPrimitiveExample() throws Exception {
.setRepetitions(100000)
.runTests();
}

private static class NoCodecExample2 {
@SuppressWarnings("unused")
private final BufferedInputStream noCodec = new BufferedInputStream(null);
}

private static class NoCodecExample1 {
@SuppressWarnings("unused")
private final NoCodecExample2 noCodec = new NoCodecExample2();
}

@Test
public void testNoCodecExample() throws Exception {
ObjectCodecs codecs = new ObjectCodecs(AutoRegistry.get(), ImmutableMap.of());
try {
codecs.serializeMemoized(new NoCodecExample1());
fail();
} catch (SerializationException.NoCodecException expected) {
assertThat(expected)
.hasMessageThat()
.contains(
"java.io.BufferedInputStream ["
+ "com.google.devtools.build.lib.skyframe.serialization."
+ "DynamicCodecTest$NoCodecExample2, "
+ "com.google.devtools.build.lib.skyframe.serialization."
+ "DynamicCodecTest$NoCodecExample1]");
}
}
}

0 comments on commit e4c64ac

Please sign in to comment.