Search in sources :

Example 1 with FrameTooLongException

use of com.datastax.oss.driver.api.core.connection.FrameTooLongException in project spring-data-cassandra by spring-projects.

the class CassandraExceptionTranslatorUnitTests method shouldTranslateFrameTooLongException.

// DATACASS-402
@Test
void shouldTranslateFrameTooLongException() {
    DataAccessException result = sut.translateExceptionIfPossible(new FrameTooLongException(socketAddress, "foo"));
    assertThat(result).isInstanceOf(CassandraUncategorizedException.class).hasCauseInstanceOf(FrameTooLongException.class);
}
Also used : FrameTooLongException(com.datastax.oss.driver.api.core.connection.FrameTooLongException) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 2 with FrameTooLongException

use of com.datastax.oss.driver.api.core.connection.FrameTooLongException in project java-driver by datastax.

the class FrameDecoder method decode.

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    int startIndex = in.readerIndex();
    if (isFirstResponse) {
        isFirstResponse = false;
        // Must read at least the protocol v1/v2 header (see below)
        if (in.readableBytes() < 8) {
            return null;
        }
        // Special case for obsolete protocol versions (< v3): the length field is at a different
        // position, so we can't delegate to super.decode() which would read the wrong length.
        int protocolVersion = (int) in.getByte(startIndex) & 0b0111_1111;
        if (protocolVersion < 3) {
            int streamId = in.getByte(startIndex + 2);
            int length = in.getInt(startIndex + 4);
            // incoming data and spoof a server-side protocol error.
            if (in.readableBytes() < 8 + length) {
                // keep reading until we can discard the whole message at once
                return null;
            } else {
                in.readerIndex(startIndex + 8 + length);
            }
            return Frame.forResponse(protocolVersion, streamId, null, Frame.NO_PAYLOAD, Collections.emptyList(), new Error(ProtocolConstants.ErrorCode.PROTOCOL_ERROR, "Invalid or unsupported protocol version"));
        }
    }
    try {
        ByteBuf buffer = (ByteBuf) super.decode(ctx, in);
        return (buffer == null) ? // did not receive whole frame yet, keep reading
        null : frameCodec.decode(buffer);
    } catch (Exception e) {
        // If decoding failed, try to read at least the stream id, so that the error can be
        // propagated to the client request matching that id (otherwise we have to fail all
        // pending requests on this channel)
        int streamId;
        try {
            streamId = in.getShort(startIndex + 2);
        } catch (Exception e1) {
            // Should never happen, super.decode does not return a non-null buffer until the length
            // field has been read, and the stream id comes before
            Loggers.warnWithException(LOG, "Unexpected error while reading stream id", e1);
            streamId = -1;
        }
        if (e instanceof TooLongFrameException) {
            // Translate the Netty error to our own type
            e = new FrameTooLongException(ctx.channel().remoteAddress(), e.getMessage());
        }
        throw new FrameDecodingException(streamId, e);
    }
}
Also used : TooLongFrameException(io.netty.handler.codec.TooLongFrameException) FrameTooLongException(com.datastax.oss.driver.api.core.connection.FrameTooLongException) Error(com.datastax.oss.protocol.internal.response.Error) ByteBuf(io.netty.buffer.ByteBuf) FrameTooLongException(com.datastax.oss.driver.api.core.connection.FrameTooLongException) TooLongFrameException(io.netty.handler.codec.TooLongFrameException)

Example 3 with FrameTooLongException

use of com.datastax.oss.driver.api.core.connection.FrameTooLongException in project java-driver by datastax.

the class FrameEncoder method encode.

@Override
protected void encode(ChannelHandlerContext ctx, Frame frame, List<Object> out) throws Exception {
    ByteBuf buffer = frameCodec.encode(frame);
    int actualLength = buffer.readableBytes();
    if (actualLength > maxFrameLength) {
        throw new FrameTooLongException(ctx.channel().remoteAddress(), String.format("Outgoing frame length exceeds %d: %d", maxFrameLength, actualLength));
    }
    out.add(buffer);
}
Also used : FrameTooLongException(com.datastax.oss.driver.api.core.connection.FrameTooLongException) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

FrameTooLongException (com.datastax.oss.driver.api.core.connection.FrameTooLongException)3 ByteBuf (io.netty.buffer.ByteBuf)2 Error (com.datastax.oss.protocol.internal.response.Error)1 TooLongFrameException (io.netty.handler.codec.TooLongFrameException)1 Test (org.junit.jupiter.api.Test)1 DataAccessException (org.springframework.dao.DataAccessException)1