I'm trying to get input from users with HTML form and send a json object to my Endpoint instance. I use Jackson for deserialization and whenever an exception happens in the decoder (for example if user enters a string instead of a number to parse) I wrap it in an unchecked one and handle it inside onError() method, but after I do the endpoint closes and I get the following error in JS console:
WebSocket is already in CLOSING or CLOSED state.
Decoder code:
public class MessageDecoder implements Decoder.Text<Message> {
@Override
public Message decode(String json) {
ObjectMapper mapper = new ObjectMapper();
Message message;
try {
message = mapper.readValue(json, Message.class);
}
catch (IOException e) {
throw new DecoderException(json, e);
}
return message;
}
@Override
public boolean willDecode(String s) {
return s != null;
}
}
onError code:
@Override
public void onError(Session session, Throwable t) {
if (t.getCause() instanceof InvalidFormatException e) {
// some unimportant lines
}
else {
LOG.error("{} at session {}, disconnecting...", t.getClass().getName(), session.getId(), t.getCause());
}
// TODO: Stop websocket from closing
}
Is there any way to prevent the websocket from closing after handling the exception or does it have to be this way in case the onError() is executed?
It happens for both annotated and programmatic endpoints with both embedded and normal Tomcat. I tried also creating my own onerror function in JS, hoping to override the existing one but it didn't fix anything.