Make all codecs throw a CodecException

- Added CodecException which is either EncoderException or
  DecoderException
- Made all decoder exceptions a subtype of DecoderException
- Replaced CodecEmbedderException with CodecException
- All abstract handlers wraps an exception with a CodecException
This commit is contained in:
Trustin Lee 2012-05-18 15:42:36 +09:00
parent 251a18160c
commit 2802b231e5
15 changed files with 188 additions and 25 deletions

View File

@ -13,41 +13,41 @@
* License for the specific language governing permissions and limitations * License for the specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package io.netty.handler.codec.embedder; package io.netty.handler.codec;
/** /**
* A {@link RuntimeException} which is thrown when a {@link CodecEmbedder} * An {@link Exception} which is thrown by a codec.
* failed to encode or decode the specified input. *
* @apiviz.exclude * @apiviz.hidden
*/ */
public class CodecEmbedderException extends RuntimeException { public class CodecException extends RuntimeException {
private static final long serialVersionUID = -6283302594160331474L; private static final long serialVersionUID = -1464830400709348473L;
/** /**
* Creates a new instance. * Creates a new instance.
*/ */
public CodecEmbedderException() { public CodecException() {
} }
/** /**
* Creates a new instance. * Creates a new instance.
*/ */
public CodecEmbedderException(String message, Throwable cause) { public CodecException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
/** /**
* Creates a new instance. * Creates a new instance.
*/ */
public CodecEmbedderException(String message) { public CodecException(String message) {
super(message); super(message);
} }
/** /**
* Creates a new instance. * Creates a new instance.
*/ */
public CodecEmbedderException(Throwable cause) { public CodecException(Throwable cause) {
super(cause); super(cause);
} }
} }

View File

@ -21,7 +21,7 @@ package io.netty.handler.codec;
* *
* @apiviz.hidden * @apiviz.hidden
*/ */
public class CorruptedFrameException extends Exception { public class CorruptedFrameException extends DecoderException {
private static final long serialVersionUID = 3918052232492988408L; private static final long serialVersionUID = 3918052232492988408L;

View File

@ -0,0 +1,53 @@
/*
* Copyright 2011 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec;
/**
* An {@link Exception} which is thrown by an encoder.
*
* @apiviz.hidden
*/
public class DecoderException extends CodecException {
private static final long serialVersionUID = 6926716840699621852L;
/**
* Creates a new instance.
*/
public DecoderException() {
}
/**
* Creates a new instance.
*/
public DecoderException(String message, Throwable cause) {
super(message, cause);
}
/**
* Creates a new instance.
*/
public DecoderException(String message) {
super(message);
}
/**
* Creates a new instance.
*/
public DecoderException(Throwable cause) {
super(cause);
}
}

View File

@ -0,0 +1,53 @@
/*
* Copyright 2011 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec;
/**
* An {@link Exception} which is thrown by an encoder.
*
* @apiviz.hidden
*/
public class EncoderException extends CodecException {
private static final long serialVersionUID = -5086121160476476774L;
/**
* Creates a new instance.
*/
public EncoderException() {
}
/**
* Creates a new instance.
*/
public EncoderException(String message, Throwable cause) {
super(message, cause);
}
/**
* Creates a new instance.
*/
public EncoderException(String message) {
super(message);
}
/**
* Creates a new instance.
*/
public EncoderException(Throwable cause) {
super(cause);
}
}

View File

@ -39,7 +39,11 @@ public abstract class MessageToMessageDecoder<I, O> extends ChannelInboundHandle
decoded = true; decoded = true;
} }
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
} }
if (decoded) { if (decoded) {

View File

@ -40,7 +40,11 @@ public abstract class MessageToMessageEncoder<I, O> extends ChannelOutboundHandl
encoded = true; encoded = true;
} }
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new EncoderException(t));
}
} }
} }

View File

@ -32,7 +32,11 @@ public abstract class MessageToStreamEncoder<I> extends ChannelOutboundHandlerAd
try { try {
encode(ctx, msg, out); encode(ctx, msg, out);
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new EncoderException(t));
}
} }
} }

View File

@ -373,7 +373,11 @@ public abstract class ReplayingDecoder<O, S extends Enum<S>> extends ChannelInbo
// Ignore // Ignore
replay.expect(REPLAY); replay.expect(REPLAY);
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
ctx.fireChannelInactive(); ctx.fireChannelInactive();
@ -426,7 +430,11 @@ public abstract class ReplayingDecoder<O, S extends Enum<S>> extends ChannelInbo
// A successful decode // A successful decode
MessageToMessageEncoder.unfoldAndAdd(ctx, ctx.nextIn(), result); MessageToMessageEncoder.unfoldAndAdd(ctx, ctx.nextIn(), result);
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
} }
} }

View File

@ -33,7 +33,11 @@ public abstract class StreamToMessageDecoder<O> extends ChannelInboundHandlerAda
ctx.fireInboundBufferUpdated(); ctx.fireInboundBufferUpdated();
} }
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
ctx.fireChannelInactive(); ctx.fireChannelInactive();
@ -66,7 +70,11 @@ public abstract class StreamToMessageDecoder<O> extends ChannelInboundHandlerAda
break; break;
} }
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
} }

View File

@ -31,7 +31,11 @@ public abstract class StreamToStreamDecoder extends ChannelInboundHandlerAdapter
try { try {
decodeLast(ctx, in, out); decodeLast(ctx, in, out);
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
if (out.readableBytes() > oldOutSize) { if (out.readableBytes() > oldOutSize) {
@ -52,7 +56,11 @@ public abstract class StreamToStreamDecoder extends ChannelInboundHandlerAdapter
try { try {
decode(ctx, in, out); decode(ctx, in, out);
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
} }
if (oldInSize == in.readableBytes()) { if (oldInSize == in.readableBytes()) {
break; break;

View File

@ -26,7 +26,11 @@ public abstract class StreamToStreamEncoder extends ChannelOutboundHandlerAdapte
try { try {
encode(ctx, in, out); encode(ctx, in, out);
} catch (Throwable t) { } catch (Throwable t) {
ctx.fireExceptionCaught(t); if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new EncoderException(t));
}
} }
if (oldInSize == in.readableBytes()) { if (oldInSize == in.readableBytes()) {
break; break;

View File

@ -20,7 +20,7 @@ package io.netty.handler.codec;
* decoded by {@link DelimiterBasedFrameDecoder} is greater than the maximum. * decoded by {@link DelimiterBasedFrameDecoder} is greater than the maximum.
* @apiviz.hidden * @apiviz.hidden
*/ */
public class TooLongFrameException extends Exception { public class TooLongFrameException extends DecoderException {
private static final long serialVersionUID = -1995801950698951640L; private static final long serialVersionUID = -1995801950698951640L;

View File

@ -23,6 +23,7 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInboundHandlerContext; import io.netty.channel.ChannelInboundHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoop; import io.netty.channel.EventLoop;
import io.netty.handler.codec.CodecException;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ConcurrentModificationException; import java.util.ConcurrentModificationException;
@ -83,15 +84,17 @@ abstract class AbstractCodecEmbedder<E> implements CodecEmbedder<E> {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private E product(Object p) { private E product(Object p) {
if (p instanceof CodecEmbedderException) { if (p instanceof CodecException) {
throw (CodecEmbedderException) p; throw (CodecException) p;
} }
if (p instanceof Throwable) { if (p instanceof Throwable) {
throw new CodecEmbedderException((Throwable) p); throw newCodecException((Throwable) p);
} }
return (E) p; return (E) p;
} }
protected abstract CodecException newCodecException(Throwable t);
@Override @Override
public final Object[] pollAll() { public final Object[] pollAll() {
final int size = size(); final int size = size();

View File

@ -20,6 +20,8 @@ import io.netty.buffer.ChannelBuffers;
import io.netty.channel.ChannelBufferHolder; import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.base64.Base64Decoder; import io.netty.handler.codec.base64.Base64Decoder;
import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringDecoder;
@ -65,4 +67,9 @@ public class DecoderEmbedder<E> extends AbstractCodecEmbedder<E> {
pipeline().fireInboundBufferUpdated(); pipeline().fireInboundBufferUpdated();
return !isEmpty(); return !isEmpty();
} }
@Override
protected CodecException newCodecException(Throwable t) {
return new DecoderException(t);
}
} }

View File

@ -18,6 +18,8 @@ package io.netty.handler.codec.embedder;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.CodecException;
import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.base64.Base64Encoder; import io.netty.handler.codec.base64.Base64Encoder;
import io.netty.handler.codec.string.StringEncoder; import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
@ -57,4 +59,9 @@ public class EncoderEmbedder<E> extends AbstractCodecEmbedder<E> {
channel().write(input); channel().write(input);
return !isEmpty(); return !isEmpty();
} }
@Override
protected CodecException newCodecException(Throwable t) {
return new EncoderException(t);
}
} }