Skip to content

Commit

Permalink
Put a trail into every SerializationException, not just NoCodecExcept…
Browse files Browse the repository at this point in the history
…ion, and have DynamicCodec add to it. It's very useful to have this trail available when debugging or whitelisting.

PiperOrigin-RevId: 201205884
  • Loading branch information
janakdr authored and Copybara-Service committed Jun 19, 2018
1 parent 2847f67 commit 9f4d5fe
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void serializeField(
} else {
try {
context.serialize(UnsafeProvider.getInstance().getObject(obj, offset), codedOut);
} catch (SerializationException.NoCodecException e) {
} catch (SerializationException e) {
e.addTrail(this.type);
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ private CodecDescriptor getDynamicCodecDescriptor(String className, @Nullable Cl
CodecDescriptor descriptor = supplier.get();
if (descriptor == null) {
throw new SerializationException.NoCodecException(
"There was a problem creating a codec for " + className + ". Check logs for details");
"There was a problem creating a codec for " + className + ". Check logs for details",
type);
}
return descriptor;
}
Expand All @@ -405,10 +406,11 @@ private CodecDescriptor getDynamicCodecDescriptor(String className, @Nullable Cl
"No default codec available for "
+ className
+ ". If this is a lambda, try casting it to (type & Serializable), like "
+ "(Supplier<String> & Serializable)");
+ "(Supplier<String> & Serializable)",
type);
}
}
throw new SerializationException.NoCodecException(
"No default codec available for " + className);
"No default codec available for " + className, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@

package com.google.devtools.build.lib.skyframe.serialization;

import com.google.common.collect.ImmutableList;
import java.io.NotSerializableException;
import java.util.ArrayList;

/** Exception signaling a failure to Serialize or Deserialize an Object. */
public class SerializationException extends Exception {
private final ArrayList<String> trail = new ArrayList<>();

public SerializationException(String msg) {
super(msg);
Expand All @@ -36,12 +38,15 @@ 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);
}

NoCodecException(String message, Class<?> type) {
super(message);
addTrail(type);
}

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

@Override
public String getMessage() {
return super.getMessage() + (trail.isEmpty() ? "" : " " + trail);
}
@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());
}
/**
* Adds extra tracing info for debugging.
*
* <p>Primarily useful for {@link DynamicCodec}.
*/
public void addTrail(Class<?> type) {
trail.add(type.getName());
}

public ImmutableList<String> getTrailForTesting() {
return ImmutableList.copyOf(trail);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ public void testNoCodecExample() throws Exception {
.hasMessageThat()
.contains(
"java.io.BufferedInputStream ["
+ "java.io.BufferedInputStream, "
+ "com.google.devtools.build.lib.skyframe.serialization."
+ "DynamicCodecTest$NoCodecExample2, "
+ "com.google.devtools.build.lib.skyframe.serialization."
Expand Down

0 comments on commit 9f4d5fe

Please sign in to comment.