From ced9b0eaca5aeb7387948c2bcd2d861ed81ec0c4 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 16 Mar 2023 22:36:58 +0100 Subject: [PATCH] Better handling of Json Exceptions in websockets --- .../src/main/java/fr/pandacube/lib/ws/AbstractWS.java | 9 ++++++--- .../main/java/fr/pandacube/lib/ws/PayloadRegistry.java | 6 ++++-- .../java/fr/pandacube/lib/ws/payloads/ErrorPayload.java | 6 ++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pandalib-ws/src/main/java/fr/pandacube/lib/ws/AbstractWS.java b/pandalib-ws/src/main/java/fr/pandacube/lib/ws/AbstractWS.java index 74ca61d..84888ff 100644 --- a/pandalib-ws/src/main/java/fr/pandacube/lib/ws/AbstractWS.java +++ b/pandalib-ws/src/main/java/fr/pandacube/lib/ws/AbstractWS.java @@ -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; } diff --git a/pandalib-ws/src/main/java/fr/pandacube/lib/ws/PayloadRegistry.java b/pandalib-ws/src/main/java/fr/pandacube/lib/ws/PayloadRegistry.java index a972080..ecf7a51 100644 --- a/pandalib-ws/src/main/java/fr/pandacube/lib/ws/PayloadRegistry.java +++ b/pandalib-ws/src/main/java/fr/pandacube/lib/ws/PayloadRegistry.java @@ -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); } diff --git a/pandalib-ws/src/main/java/fr/pandacube/lib/ws/payloads/ErrorPayload.java b/pandalib-ws/src/main/java/fr/pandacube/lib/ws/payloads/ErrorPayload.java index 37ca2f6..6c69902 100644 --- a/pandalib-ws/src/main/java/fr/pandacube/lib/ws/payloads/ErrorPayload.java +++ b/pandalib-ws/src/main/java/fr/pandacube/lib/ws/payloads/ErrorPayload.java @@ -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")