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

  • Issue Imported From: https://github.com/tyrus-project/tyrus/issues/271
  • Original Issue Raised By:@glassfishrobot
  • Original Issue Assigned To: @pavelbucek

glassfishrobot avatar Feb 10 '18 19:02 glassfishrobot

@glassfishrobot Commented Reported by mikc22

glassfishrobot avatar Feb 14 '13 12:02 glassfishrobot

@glassfishrobot Commented mikc22 said: If the user thows the exception (e.g. in onClose method) the behavior differs in the programmatic and annotated case. In the annotated case the user doesn't get the exceptions as-is (unlike in Programmatic case) and it's wrapped in java.lang.reflect.InvocationTargetException, see the following execption example and the comparison to the programmatic case.

cd tyrus~source-code-repository/trunk/tests/qa/websockets-lifecycle-test && mvn -Dtest=LifeCycleTest#testLifeCycleAnnotated clean test

Annotated: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.glassfish.tyrus.core.AnnotatedEndpoint.callMethod(AnnotatedEndpoint.java:439) at org.glassfish.tyrus.core.AnnotatedEndpoint.onClose(AnnotatedEndpoint.java:448) ...snip... Caused by: java.lang.RuntimeException: going onError at org.glassfish.tyrus.tests.qa.lifecycle.SessionLifeCycle.onServerClose(SessionLifeCycle.java:87) at org.glassfish.tyrus.tests.qa.lifecycle.handlers.text.AnnotatedStringSession$Server.onClose(AnnotatedStringSession.java:87) ... 27 more

Programmatic cd tyrus~source-code-repository/trunk/tests/qa/websockets-lifecycle-test && mvn -Dtest=LifeCycleTest#testLifeCyeProgrammatic clean test java.lang.RuntimeException: going onError at org.glassfish.tyrus.tests.qa.lifecycle.SessionLifeCycle.onServerClose(SessionLifeCycle.java:87) at org.glassfish.tyrus.tests.qa.lifecycle.ProgrammaticEndpoint.onClose(ProgrammaticEndpoint.java:100) at org.glassfish.tyrus.core.EndpointWrapper.onClose(EndpointWrapper.java:498) at org.glassfish.tyrus.server.TyrusEndpoint.onClose(TyrusEndpoint.java:181) at org.glassfish.tyrus.websockets.DefaultWebSocket.onClose(DefaultWebSocket.java:95) at org.glassfish.tyrus.websockets.frametypes.ClosingFrameType.respond(ClosingFrameType.java:57) at org.glassfish.tyrus.websockets.DataFrame.respond(DataFrame.java:102) ...snip...

glassfishrobot avatar Feb 27 '13 16:02 glassfishrobot

@glassfishrobot Commented This issue was imported from java.net JIRA TYRUS-94

glassfishrobot avatar Apr 25 '17 02:04 glassfishrobot