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 84888ff..4067d12 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 @@ -32,15 +32,26 @@ public interface AbstractWS { return; } - try { - onReceivePayload(payload); - } catch (Throwable t) { - trySendAsJson(new ErrorPayload("Error handling payload: " + t)); - if (t instanceof Exception) - logError("Error handling payload", t); - else - throw t; + if (payload instanceof ErrorPayload errorPayload) { + try { + onReceiveErrorPayload(errorPayload); + } catch(Exception e) { + logError("Error while handling received error payload", e); + } } + else { + try { + onReceivePayload(payload); + } catch (Throwable t) { + trySendAsJson(new ErrorPayload("Error while handling your payload: " + t)); + if (t instanceof Exception) + logError("Error while handling received payload", t); + else + throw t; + } + } + + } /** @@ -62,6 +73,15 @@ public interface AbstractWS { */ void onReceivePayload(Payload payload); + /** + * Called on reception of a valid {@link ErrorPayload}. + * @param error the received {@link ErrorPayload}. + * @implNote default implementation will log the received error. + */ + default void onReceiveErrorPayload(ErrorPayload error) { + logError("Received the following error from the remote side: " + error.message, error.throwable); + } + /** * Called on reception of a websocket Close packet. * The connection is closed after this method call. 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 ecf7a51..ad922a7 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 @@ -49,12 +49,12 @@ public class PayloadRegistry { public static Payload fromString(String message) { String[] split = message.split(Pattern.quote(PAYLOAD_TYPE_SEPARATOR), 2); if (split.length != 2) { - throw new IllegalArgumentException("Malformed message: does not respect format '" + PAYLOAD_TYPE_SEPARATOR + "'."); + throw new IllegalArgumentException("Malformed message: does not respect format ‘" + PAYLOAD_TYPE_SEPARATOR + "’."); } Class detectedClass = payloadClasses.get(split[0]); if (detectedClass == null) { - throw new IllegalArgumentException("Unrecognized data type '" + split[0] + "'."); + throw new IllegalArgumentException("Unrecognized data type ‘" + split[0] + "’."); } try {