dev@grizzly.java.net

Re: how throw/return exception in a ProtocolParser ?

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Fri, 14 Nov 2008 15:14:22 +0100

On Nov 14, 2008, at 14:32 , Survivant 00 wrote:

> In ym ProtocolParser in
>
> public boolean hasNextMessage() { ... }
>
>
> I'm decoding the query received.
>
> try {
> ByteBuffer tmp = processingBuffer.duplicate();
> msg = f_asciiDecoder.decode(tmp).toString();
> } catch (CharacterCodingException e) {
> s_logger.error("Error decoding query",e);
> return false;
> }
>
> if I catch a exception, I would like to return a message to the
> client : "Error processing the query..." (I could be any kind of
> exceptions)
>
> how can I do that ?
>
> I could return a message in
>
> public String getNextMessage() {
>
> if(s_logger.isDebugEnabled()){
> s_logger.debug("getNextMessage");
> }
>
> if(exception){
> return "Error processing the query...";
> }
> return query;
> }
>
> but if I do that (and I don't like it), I will have to test each
> query return.

In your case, IMHO, it makes sense to change ProtocolParser
implementation, so it will not return String message, but let's say
Result:
public class Result {
          public enum Status {OK, ERROR};
          private Status status;
          private String message;

         getters/setters
}

So the code will look more clear.

Thanks.

WBR,
Alexey.