eclipse-ee4j / eclipse-ee4j/tyrus

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

Open
#271 4 comments 0 reactions 0 assignees View on GitHub
Priority: Major Type: Improvement
Dominant language
Java
Stars
128
Forks
49
PR merge metrics
No merged PRs in 30d

Description

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]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.