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
* under the License.
*/
package io.netty.handler.codec.embedder;
package io.netty.handler.codec;
/**
* A {@link RuntimeException} which is thrown when a {@link CodecEmbedder}
* failed to encode or decode the specified input.
* @apiviz.exclude
* An {@link Exception} which is thrown by a codec.
*
* @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.
*/
public CodecEmbedderException() {
public CodecException() {
}
/**
* Creates a new instance.
*/
public CodecEmbedderException(String message, Throwable cause) {
public CodecException(String message, Throwable cause) {
super(message, cause);
}
/**
* Creates a new instance.
*/
public CodecEmbedderException(String message) {
public CodecException(String message) {
super(message);
}
/**
* Creates a new instance.
*/
public CodecEmbedderException(Throwable cause) {
public CodecException(Throwable cause) {
super(cause);
}
}

View File

@ -21,7 +21,7 @@ package io.netty.handler.codec;
*
* @apiviz.hidden
*/
public class CorruptedFrameException extends Exception {
public class CorruptedFrameException extends DecoderException {
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;
}
} catch (Throwable t) {
ctx.fireExceptionCaught(t);
if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
}
}
if (decoded) {

View File

@ -40,7 +40,11 @@ public abstract class MessageToMessageEncoder<I, O> extends ChannelOutboundHandl
encoded = true;
}
} 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 {
encode(ctx, msg, out);
} 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
replay.expect(REPLAY);
} catch (Throwable t) {
ctx.fireExceptionCaught(t);
if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
}
ctx.fireChannelInactive();
@ -426,7 +430,11 @@ public abstract class ReplayingDecoder<O, S extends Enum<S>> extends ChannelInbo
// A successful decode
MessageToMessageEncoder.unfoldAndAdd(ctx, ctx.nextIn(), result);
} 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();
}
} catch (Throwable t) {
ctx.fireExceptionCaught(t);
if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
}
ctx.fireChannelInactive();
@ -66,7 +70,11 @@ public abstract class StreamToMessageDecoder<O> extends ChannelInboundHandlerAda
break;
}
} 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 {
decodeLast(ctx, in, out);
} catch (Throwable t) {
ctx.fireExceptionCaught(t);
if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
}
if (out.readableBytes() > oldOutSize) {
@ -52,7 +56,11 @@ public abstract class StreamToStreamDecoder extends ChannelInboundHandlerAdapter
try {
decode(ctx, in, out);
} catch (Throwable t) {
ctx.fireExceptionCaught(t);
if (t instanceof CodecException) {
ctx.fireExceptionCaught(t);
} else {
ctx.fireExceptionCaught(new DecoderException(t));
}
}
if (oldInSize == in.readableBytes()) {
break;

View File

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

View File

@ -20,7 +20,7 @@ package io.netty.handler.codec;
* decoded by {@link DelimiterBasedFrameDecoder} is greater than the maximum.
* @apiviz.hidden
*/
public class TooLongFrameException extends Exception {
public class TooLongFrameException extends DecoderException {
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.ChannelPipeline;
import io.netty.channel.EventLoop;
import io.netty.handler.codec.CodecException;
import java.lang.reflect.Array;
import java.util.ConcurrentModificationException;
@ -83,15 +84,17 @@ abstract class AbstractCodecEmbedder<E> implements CodecEmbedder<E> {
@SuppressWarnings("unchecked")
private E product(Object p) {
if (p instanceof CodecEmbedderException) {
throw (CodecEmbedderException) p;
if (p instanceof CodecException) {
throw (CodecException) p;
}
if (p instanceof Throwable) {
throw new CodecEmbedderException((Throwable) p);
throw newCodecException((Throwable) p);
}
return (E) p;
}
protected abstract CodecException newCodecException(Throwable t);
@Override
public final Object[] pollAll() {
final int size = size();

View File

@ -20,6 +20,8 @@ import io.netty.buffer.ChannelBuffers;
import io.netty.channel.ChannelBufferHolder;
import io.netty.channel.ChannelHandler;
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.string.StringDecoder;
@ -65,4 +67,9 @@ public class DecoderEmbedder<E> extends AbstractCodecEmbedder<E> {
pipeline().fireInboundBufferUpdated();
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.channel.ChannelHandler;
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.string.StringEncoder;
import io.netty.util.CharsetUtil;
@ -57,4 +59,9 @@ public class EncoderEmbedder<E> extends AbstractCodecEmbedder<E> {
channel().write(input);
return !isEmpty();
}
@Override
protected CodecException newCodecException(Throwable t) {
return new EncoderException(t);
}
}