Skip to content

error message transformer #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions reactivesocket-core/src/main/java/io/reactivesocket/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.reactivesocket;

import io.reactivesocket.internal.*;
import io.reactivesocket.internal.frame.ErrorFrameFlyweight;
import io.reactivesocket.internal.frame.FrameHeaderFlyweight;
import io.reactivesocket.internal.frame.FramePool;
Expand All @@ -27,8 +26,12 @@
import org.agrona.DirectBuffer;
import org.agrona.MutableDirectBuffer;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;

import static java.lang.System.getProperty;

Expand Down Expand Up @@ -321,6 +324,38 @@ public static String dataMimeType(final Frame frame)
public static class Error
{

private static final Function<Throwable, ByteBuffer> DEFAULT_ERROR_MESSAGE_TRANSFORMER;

// TODO Make this plugin, and also make it so that a error message transformer can be
// TODO passed in with an application
static {
if (Boolean.getBoolean("io.reactivesocket.debugError")) {
DEFAULT_ERROR_MESSAGE_TRANSFORMER = throwable -> {
StringWriter writer = new StringWriter();

String message = throwable.getMessage() == null
? ""
: throwable.getMessage() + "\n";

writer.write(message);

PrintWriter printWriter = new PrintWriter(writer);
throwable.printStackTrace(printWriter);
String data = writer.toString();
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
final ByteBuffer dataBuffer = ByteBuffer.wrap(bytes);
return dataBuffer;
};
} else {
DEFAULT_ERROR_MESSAGE_TRANSFORMER = throwable -> {
String data = throwable.getMessage() == null ? "" : throwable.getMessage();
byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
final ByteBuffer dataBuffer = ByteBuffer.wrap(bytes);
return dataBuffer;
};
}
}

private Error() {}

public static Frame from(
Expand All @@ -343,11 +378,7 @@ public static Frame from(
final Throwable throwable,
ByteBuffer metadata
) {
String data = throwable.getMessage() == null ? "" : throwable.getMessage();
byte[] bytes = data.getBytes(Charset.forName("UTF-8"));
final ByteBuffer dataBuffer = ByteBuffer.wrap(bytes);

return from(streamId, throwable, metadata, dataBuffer);
return from(streamId, throwable, metadata, DEFAULT_ERROR_MESSAGE_TRANSFORMER.apply(throwable));
}

public static Frame from(
Expand Down