static Name fromProtocolId(int id) { Name name = nameToIds[id]; if (name == null) throw new DriverInternalError("Unknown data type protocol id: " + id); return name; }
DriverInternalError unsupported() { return new DriverInternalError("Unsupported protocol version " + this); }
private Message.ProtocolEncoder messageEncoderFor(ProtocolVersion version) { switch (version) { case V1: return messageEncoderV1; case V2: return messageEncoderV2; case V3: return messageEncoderV3; case V4: return messageEncoderV4; case V5: return messageEncoderV5; default: throw new DriverInternalError("Unsupported protocol version " + protocolVersion); } } }
static Kind fromId(int id) { Kind k = ids[id]; if (k == null) throw new DriverInternalError(String.format("Unknown kind id %d in RESULT message", id)); return k; } }
static Type fromOpcode(int opcode) { if (opcode < 0 || opcode >= opcodeIdx.length) throw new DriverInternalError(String.format("Unknown response opcode %d", opcode)); Type t = opcodeIdx[opcode]; if (t == null) throw new DriverInternalError(String.format("Unknown response opcode %d", opcode)); return t; } }
static ConsistencyLevel fromCode(int code) { if (code < 0 || code >= codeIdx.length) throw new DriverInternalError(String.format("Unknown code %d for a consistency level", code)); return codeIdx[code]; }
private void throwSyntaxError(String msg) { throw new DriverInternalError( String.format("Syntax error parsing '%s' at char %d: %s", str, idx, msg)); }
public static ExceptionCode fromValue(int value) { ExceptionCode code = valueToCode.get(value); if (code == null) throw new DriverInternalError(String.format("Unknown error code %d", value)); return code; } }
public static InetAddress readInetWithoutPort(ByteBuf cb) { int addrSize = cb.readByte() & 0xFF; byte[] address = new byte[addrSize]; cb.readBytes(address); try { return InetAddress.getByAddress(address); } catch (UnknownHostException e) { throw new DriverInternalError( String.format( "Invalid IP address (%d.%d.%d.%d) while deserializing inet address", address[0], address[1], address[2], address[3])); } }
public static InetSocketAddress readInet(ByteBuf cb) { int addrSize = cb.readByte() & 0xFF; byte[] address = new byte[addrSize]; cb.readBytes(address); int port = cb.readInt(); try { return new InetSocketAddress(InetAddress.getByAddress(address), port); } catch (UnknownHostException e) { throw new DriverInternalError( String.format( "Invalid IP address (%d.%d.%d.%d) while deserializing inet address", address[0], address[1], address[2], address[3])); } }
public static String readLongString(ByteBuf cb) { try { int length = cb.readInt(); return readString(cb, length); } catch (IndexOutOfBoundsException e) { throw new DriverInternalError( "Not enough bytes to read an UTF8 serialized string preceded by it's 4 bytes length"); } }
public static <T extends Enum<T>> T readEnumValue(Class<T> enumType, ByteBuf cb) { String value = CBUtil.readString(cb); try { return Enum.valueOf(enumType, value.toUpperCase()); } catch (IllegalArgumentException e) { throw new DriverInternalError( String.format("Invalid value '%s' for %s", value, enumType.getSimpleName())); } }
@Override public DriverInternalError copy() { return new DriverInternalError(getMessage(), this); } }
private static String readString(ByteBuf cb, int length) { try { String str = cb.toString(cb.readerIndex(), length, CharsetUtil.UTF_8); cb.readerIndex(cb.readerIndex() + length); return str; } catch (IllegalStateException e) { // That's the way netty encapsulate a CCE if (e.getCause() instanceof CharacterCodingException) throw new DriverInternalError("Cannot decode string as UTF8"); else throw e; } }
public static byte[] readBytes(ByteBuf cb) { try { int length = cb.readUnsignedShort(); byte[] bytes = new byte[length]; cb.readBytes(bytes); return bytes; } catch (IndexOutOfBoundsException e) { throw new DriverInternalError( "Not enough bytes to read a byte array preceded by it's 2 bytes length"); } }
static RuntimeException propagateCause(ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof Error) throw ((Error) cause); // We could just rethrow e.getCause(). However, the cause of the ExecutionException has likely // been // created on the I/O thread receiving the response. Which means that the stacktrace associated // with said cause will make no mention of the current thread. This is painful for say, finding // out which execute() statement actually raised the exception. So instead, we re-create the // exception. if (cause instanceof DriverException) throw ((DriverException) cause).copy(); else throw new DriverInternalError("Unexpected exception thrown", cause); } }
public static String readString(ByteBuf cb) { try { int length = cb.readUnsignedShort(); return readString(cb, length); } catch (IndexOutOfBoundsException e) { throw new DriverInternalError( "Not enough bytes to read an UTF8 serialized string preceded by it's 2 bytes length"); } }
@Override public Token getToken(int i) { if (tokenFactory == null) throw new DriverInternalError( "Token factory not set. This should only happen at initialization time"); checkType(i, tokenFactory.getTokenType().getName()); ByteBuffer value = data.get(i); if (value == null || value.remaining() == 0) return null; return tokenFactory.deserialize(value, protocolVersion); }
private static GuavaCompatibility selectImplementation() { if (isGuava_19_0_OrHigher()) { logger.info("Detected Guava >= 19 in the classpath, using modern compatibility layer"); return new Version19OrHigher(); } else if (isGuava_16_0_1_OrHigher()) { logger.info("Detected Guava < 19 in the classpath, using legacy compatibility layer"); return new Version18OrLower(); } else { throw new DriverInternalError( "Detected incompatible version of Guava in the classpath. " + "You need 16.0.1 or higher."); } }
/** Tests DriverInternalError. Tests basic message, rethrow, and copy abilities. */ @Test(groups = "unit") public void driverInternalError() throws Exception { String errorMessage = "Test Message"; try { throw new DriverInternalError(errorMessage); } catch (DriverInternalError e1) { try { throw new DriverInternalError(e1); } catch (DriverInternalError e2) { assertTrue(e2.getMessage().contains(errorMessage)); DriverInternalError copy = (DriverInternalError) e2.copy(); assertEquals(copy.getMessage(), e2.getMessage()); } } }