Better handling of Json Exceptions in websockets

This commit is contained in:
Marc Baloup 2023-03-16 22:36:58 +01:00
parent 4ec47b5e4b
commit ced9b0eaca
3 changed files with 12 additions and 9 deletions

View File

@ -1,5 +1,6 @@
package fr.pandacube.lib.ws; package fr.pandacube.lib.ws;
import com.google.gson.JsonParseException;
import fr.pandacube.lib.util.Log; import fr.pandacube.lib.util.Log;
import fr.pandacube.lib.util.ThrowableUtil.RunnableException; import fr.pandacube.lib.util.ThrowableUtil.RunnableException;
import fr.pandacube.lib.ws.payloads.ErrorPayload; import fr.pandacube.lib.ws.payloads.ErrorPayload;
@ -104,9 +105,10 @@ public interface AbstractWS {
* @param obj the object to Jsonify. * @param obj the object to Jsonify.
* @param serializeNulls if null propreties must be included in the json object. * @param serializeNulls if null propreties must be included in the json object.
* @throws IOException if an IO error occurs when sending the data. * @throws IOException if an IO error occurs when sending the data.
* @throws JsonParseException if the json is invalid.
* @see PayloadRegistry#arbitraryToString(String, Object, boolean) * @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)); sendString(PayloadRegistry.arbitraryToString(type, obj, serializeNulls));
} }
@ -127,9 +129,10 @@ public interface AbstractWS {
* Send the provided {@link Payload} to the remote endpoint. * Send the provided {@link Payload} to the remote endpoint.
* @param payload the {@link Payload} to send. * @param payload the {@link Payload} to send.
* @throws IOException if an IO error occurs when sending the data. * @throws IOException if an IO error occurs when sending the data.
* @throws JsonParseException if the json is invalid.
* @see PayloadRegistry#toString(Payload) * @see PayloadRegistry#toString(Payload)
*/ */
default void sendAsJson(Payload payload) throws IOException { default void sendAsJson(Payload payload) throws IOException, JsonParseException {
sendString(PayloadRegistry.toString(payload)); sendString(PayloadRegistry.toString(payload));
} }
@ -208,7 +211,7 @@ public interface AbstractWS {
try { try {
run.run(); run.run();
return true; return true;
} catch (IOException e) { } catch (IOException|JsonParseException e) {
logError(errorMessage, e); logError(errorMessage, e);
return false; return false;
} }

View File

@ -68,8 +68,9 @@ public class PayloadRegistry {
* Serialize the provided {@link Payload}. * Serialize the provided {@link Payload}.
* @param p the {@link Payload} to serialize. Must be of a registered type. * @param p the {@link Payload} to serialize. Must be of a registered type.
* @return the serialized data. * @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()); String type = payloadClasses.getKey(p.getClass());
if (type == null) if (type == null)
throw new IllegalArgumentException(p.getClass() + " is not a registered payload type."); 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 obj the object to Jsonify
* @param serializeNulls if null propreties must be included in the json object. * @param serializeNulls if null propreties must be included in the json object.
* @return the String to send through the websocket * @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); return validateType(type) + PAYLOAD_TYPE_SEPARATOR + (serializeNulls ? Json.gsonSerializeNulls : Json.gson).toJson(obj);
} }

View File

@ -1,7 +1,5 @@
package fr.pandacube.lib.ws.payloads; package fr.pandacube.lib.ws.payloads;
import fr.pandacube.lib.util.ThrowableUtil;
/** /**
* Error message payload. * Error message payload.
*/ */
@ -13,7 +11,7 @@ public class ErrorPayload extends Payload {
/** /**
* The error Throwable, may be null. * The error Throwable, may be null.
*/ */
public String exception; public Throwable throwable;
/** /**
* Initialize an error payload with a message but not 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) { public ErrorPayload(String message, Throwable throwable) {
this.message = message; this.message = message;
this.exception = ThrowableUtil.stacktraceToString(throwable); this.throwable = throwable;
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")