1

I have a typical Spring endpoint that serves as a Websocket topic to send messages to (as can be seen in examples at Sending Error message in Spring websockets).

For handling malformed messages that can not be parsed I have created

@MessageExceptionHandler()
public void errorHandler(Exception e, @Headers Map<String, Object> headers) {
    LOGGER.error("Bad Packet received: ", e);
}

However I like to have a byte array with the original malformed message, so I can see what exactly was wrong. How can I get it?

onkami
  • 8,791
  • 17
  • 90
  • 176

1 Answers1

0

Exception type works as a linked exception list. I implemented a similar method do get the exactly exception that initiated the excepetions.

@ExceptionHandler({ Exception.class })
    public @ResponseBody String handleException(Exception ex) {
        Throwable cause = getContaExceptionIfExists(ex);
        return getMessageFromException(cause.getMessage());
    }

    public String getMessageFromException(String message) {
        return StringUtils.substringBetween(message , "interpolatedMessage='", "'");
    }

    public Throwable getContaExceptionIfExists(Exception ex) {
        Throwable cause = ex;       
        do 
        {
            if (cause instanceof ContaExcepetion)
            {
                return cause;
            }
            cause = cause.getCause();
        } 
        while (cause != null);  
        return ex;
    }

This sample code is handler every exceptions in my project. And I'm check if theres any excepetion with type "ContaExcepetion".

Adolfok3
  • 304
  • 3
  • 14
  • I do know the exception itself, I like to see the bytes of message that caused it. In Spring, when you make an endpoint, you specify the parameters as object, and Spring parses JSON to this object below the hood. But if you get a malformed message, you therefore do not have your endpoint called, and the method I quoted is triggered instead, if specified. However I do not know if the message as bytes is passed to it. – onkami Jun 29 '18 at 08:16