tyrus icon indicating copy to clipboard operation
tyrus copied to clipboard

ServerEndPoint: onError(): throwable.getCause()==null

Open glassfishrobot opened this issue 12 years ago • 4 comments

I'm getting NPE in the following code. In the onClose() method I send a message, however the socket is already closed, this will raise the onError() method which is fine, however when I want to determine the cause via Throwable.getCause() I get null pointer. It would be handy if we can somehow determine what happened.

public class ProgrammaticServer extends Endpoint {

    private static final Logger logger = Logger.getLogger(ProgrammaticServer.class.getCanonicalName());
    BasicTextMessageHandler mh;

    @Override
    public void onOpen(Session s, EndpointConfiguration ec) {
        logger.log(Level.INFO, "Someone connected:{0}", s.getRequestURI().toString());
        mh = ((ProgrammaticServerConfiguration)ec).getMessageHandler("messageHandler");
        mh.setSession(s);
        s.addMessageHandler(mh);
    }

    @Override
    public void onClose(Session s, CloseReason reason) {
        logger.log(Level.INFO, "Clossing the session: {0}", s.toString());
        final RemoteEndpoint remote = s.getRemote();
        try {

            if(!reason.getCloseCode().equals(CloseReason.CloseCodes.GOING_AWAY)) {
throw new RuntimeException("CloseReason.CloseCode should be GOING_AWAY");
            }
            //should raise on error
            remote.sendString("Raise onError now - socket is closed");
            s.close();
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void onError(Session s, Throwable thr) {
        logger.log(Level.SEVERE, "onError: {0}", thr.getLocalizedMessage());
        logger.log(Level.SEVERE, "onError: {0}", thr.getMessage());
        logger.log(Level.SEVERE, "onError: cause: {0}", thr.getCause().getMessage());
        final RemoteEndpoint remote = s.getRemote();
        try {
            remote.sendString("onError");
        } catch (IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }
}

Environment

uname -a Linux mikc 3.2.0-27-generic #43-Ubuntu SMP Fri Jul 6 14:25:57 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

Affected Versions

[1.0-b12]

glassfishrobot avatar Feb 14 '13 12:02 glassfishrobot