0

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.

2
  • Please provide enough code so others can better understand or reproduce the problem. Commented Jul 28, 2022 at 15:29
  • Edited and added codes for decoder and onError, thanks. Commented Jul 29, 2022 at 9:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.