Skip to content

Commit

Permalink
Fix GatewayException.printStackTrace(PrintStream) (#763)
Browse files Browse the repository at this point in the history
The printStackTrace() implementation taking a PrintStream (rather than a
PrintWriter) in the Java GatewayException class failed to print output.
This was due to the PrinterWriter used to wrap the supplied PrintStream
not being flushed.

This change calls the PrintStream overload with unit tests, instead of
the PrintWriter overload (to which it defers) to ensure both variants
behave correctly. The printStackTrace() implementation is updated to
correctly flush the PrintWriter used internally to wrap the supplied
PrintStream.

Signed-off-by: Mark S. Lewis <[email protected]>
  • Loading branch information
bestbeforetoday authored Oct 18, 2024
1 parent d19022b commit 102126e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public void printStackTrace() {
*/
@Override
public void printStackTrace(final PrintStream out) {
printStackTrace(new PrintWriter(out));
PrintWriter writer = new PrintWriter(out);
printStackTrace(writer);
writer.flush();
}

/**
Expand All @@ -76,7 +78,6 @@ public void printStackTrace(final PrintWriter out) {

try (PrintWriter printer = new PrintWriter(message)) {
super.printStackTrace(printer);
printer.flush();
}

List<ErrorDetail> details = getDetails();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
import com.google.rpc.Code;
import io.grpc.StatusRuntimeException;
import io.grpc.protobuf.StatusProto;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -37,10 +38,9 @@ void error_details_are_printed() {
.build());
GatewayException e = new GatewayException(newStatusRuntimeException(Code.ABORTED, "STATUS_MESSAGE", details));

CharArrayWriter actual = new CharArrayWriter();
try (PrintWriter out = new PrintWriter(actual)) {
ByteArrayOutputStream actual = new ByteArrayOutputStream();
try (PrintStream out = new PrintStream(actual)) {
e.printStackTrace(out);
out.flush();
}

List<String> expected = details.stream()
Expand All @@ -49,6 +49,33 @@ void error_details_are_printed() {
assertThat(actual.toString()).contains(expected);
}

@Test
void message_from_StatusRuntimeException_is_printed() {
GatewayException e = new GatewayException(
newStatusRuntimeException(Code.ABORTED, "STATUS_MESSAGE", Collections.emptyList()));

ByteArrayOutputStream actual = new ByteArrayOutputStream();
try (PrintStream out = new PrintStream(actual)) {
e.printStackTrace(out);
}

String expected = e.getCause().getLocalizedMessage();
assertThat(actual.toString()).contains(expected);
}

@Test
void print_stream_passed_to_printStackTrace_not_closed() {
GatewayException e = new GatewayException(newStatusRuntimeException(Code.ABORTED, "", Collections.emptyList()));

ByteArrayOutputStream actual = new ByteArrayOutputStream();
try (PrintStream out = new PrintStream(actual)) {
e.printStackTrace(out);
out.println("EXPECTED_SUBSEQUENT_MESSAGE");
}

assertThat(actual.toString()).contains("EXPECTED_SUBSEQUENT_MESSAGE");
}

private StatusRuntimeException newStatusRuntimeException(Code code, String message, List<ErrorDetail> details) {
List<Any> anyDetails = details.stream().map(Any::pack).collect(Collectors.toList());
com.google.rpc.Status status = com.google.rpc.Status.newBuilder()
Expand Down

0 comments on commit 102126e

Please sign in to comment.