Better handling of Json Exceptions in websockets
This commit is contained in:
parent
4ec47b5e4b
commit
ced9b0eaca
@ -1,5 +1,6 @@
|
||||
package fr.pandacube.lib.ws;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
import fr.pandacube.lib.util.Log;
|
||||
import fr.pandacube.lib.util.ThrowableUtil.RunnableException;
|
||||
import fr.pandacube.lib.ws.payloads.ErrorPayload;
|
||||
@ -104,9 +105,10 @@ public interface AbstractWS {
|
||||
* @param obj the object to Jsonify.
|
||||
* @param serializeNulls if null propreties must be included in the json object.
|
||||
* @throws IOException if an IO error occurs when sending the data.
|
||||
* @throws JsonParseException if the json is invalid.
|
||||
* @see PayloadRegistry#arbitraryToString(String, Object, boolean)
|
||||
*/
|
||||
default void sendAsJson(String type, Object obj, boolean serializeNulls) throws IOException {
|
||||
default void sendAsJson(String type, Object obj, boolean serializeNulls) throws IOException, JsonParseException {
|
||||
sendString(PayloadRegistry.arbitraryToString(type, obj, serializeNulls));
|
||||
}
|
||||
|
||||
@ -127,9 +129,10 @@ public interface AbstractWS {
|
||||
* Send the provided {@link Payload} to the remote endpoint.
|
||||
* @param payload the {@link Payload} to send.
|
||||
* @throws IOException if an IO error occurs when sending the data.
|
||||
* @throws JsonParseException if the json is invalid.
|
||||
* @see PayloadRegistry#toString(Payload)
|
||||
*/
|
||||
default void sendAsJson(Payload payload) throws IOException {
|
||||
default void sendAsJson(Payload payload) throws IOException, JsonParseException {
|
||||
sendString(PayloadRegistry.toString(payload));
|
||||
}
|
||||
|
||||
@ -208,7 +211,7 @@ public interface AbstractWS {
|
||||
try {
|
||||
run.run();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
} catch (IOException|JsonParseException e) {
|
||||
logError(errorMessage, e);
|
||||
return false;
|
||||
}
|
||||
|
@ -68,8 +68,9 @@ public class PayloadRegistry {
|
||||
* Serialize the provided {@link Payload}.
|
||||
* @param p the {@link Payload} to serialize. Must be of a registered type.
|
||||
* @return the serialized data.
|
||||
* @throws JsonParseException if the json is invalid.
|
||||
*/
|
||||
public static String toString(Payload p) {
|
||||
public static String toString(Payload p) throws JsonParseException {
|
||||
String type = payloadClasses.getKey(p.getClass());
|
||||
if (type == null)
|
||||
throw new IllegalArgumentException(p.getClass() + " is not a registered payload type.");
|
||||
@ -84,8 +85,9 @@ public class PayloadRegistry {
|
||||
* @param obj the object to Jsonify
|
||||
* @param serializeNulls if null propreties must be included in the json object.
|
||||
* @return the String to send through the websocket
|
||||
* @throws JsonParseException if the json is invalid.
|
||||
*/
|
||||
public static String arbitraryToString(String type, Object obj, boolean serializeNulls) {
|
||||
public static String arbitraryToString(String type, Object obj, boolean serializeNulls) throws JsonParseException {
|
||||
return validateType(type) + PAYLOAD_TYPE_SEPARATOR + (serializeNulls ? Json.gsonSerializeNulls : Json.gson).toJson(obj);
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,5 @@
|
||||
package fr.pandacube.lib.ws.payloads;
|
||||
|
||||
import fr.pandacube.lib.util.ThrowableUtil;
|
||||
|
||||
/**
|
||||
* Error message payload.
|
||||
*/
|
||||
@ -13,7 +11,7 @@ public class ErrorPayload extends Payload {
|
||||
/**
|
||||
* The error Throwable, may be null.
|
||||
*/
|
||||
public String exception;
|
||||
public Throwable throwable;
|
||||
|
||||
/**
|
||||
* Initialize an error payload with a message but not throwable.
|
||||
@ -30,7 +28,7 @@ public class ErrorPayload extends Payload {
|
||||
*/
|
||||
public ErrorPayload(String message, Throwable throwable) {
|
||||
this.message = message;
|
||||
this.exception = ThrowableUtil.stacktraceToString(throwable);
|
||||
this.throwable = throwable;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
Loading…
Reference in New Issue
Block a user