Skip to content

Commit

Permalink
Improve assertion error message
Browse files Browse the repository at this point in the history
Simplify Exceptions.stringify implementation.
  • Loading branch information
davidegrohmann committed Sep 28, 2016
1 parent a3f2808 commit a37ee3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
21 changes: 9 additions & 12 deletions community/kernel/src/main/java/org/neo4j/helpers/Exceptions.java
Expand Up @@ -21,6 +21,8 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.Thread.State;
import java.lang.reflect.Field;
Expand Down Expand Up @@ -150,21 +152,16 @@ public static Throwable rootCause( Throwable caughtException )
return root;
}

public static String stringify( Throwable cause )
public static String stringify( Throwable throwable )
{
try
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
PrintStream target = new PrintStream( bytes, true, UTF_8.name() );
cause.printStackTrace( target );
target.flush();
return bytes.toString( UTF_8.name());
}
catch ( UnsupportedEncodingException e )
if ( throwable == null )
{
cause.printStackTrace(System.err);
return "[ERROR: Unable to serialize stacktrace, UTF-8 not supported.]";
return null;
}

StringWriter stringWriter = new StringWriter();
throwable.printStackTrace( new PrintWriter( stringWriter ) );
return stringWriter.toString();
}

public static String stringify( Thread thread, StackTraceElement[] elements )
Expand Down
Expand Up @@ -41,6 +41,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.neo4j.helpers.Exceptions.stringify;
import static org.neo4j.kernel.impl.ha.ClusterManager.allSeesAllAsAvailable;
import static org.neo4j.kernel.impl.ha.ClusterManager.instanceEvicted;

Expand Down Expand Up @@ -124,7 +125,7 @@ public void run()
thread.join();

Throwable throwable = ref.get();
assertNull( "" + throwable, throwable );
assertNull( stringify( throwable ), throwable );
}

private void takeExclusiveLockOnSameNodeAfterSwitch( Label testLabel, HighlyAvailableGraphDatabase master,
Expand Down

0 comments on commit a37ee3b

Please sign in to comment.