Source:
<code>public static Slice castFromVarcharToIpPrefix(@SqlType("varchar(x)") Slice slice) { byte[] address; int subnetSize; String string = slice.toStringUtf8(); if (!string.contains("/")) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } String[] parts = string.split("/"); try { address = InetAddresses.forString(parts[0]).getAddress(); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } subnetSize = Integer.parseInt(parts[1]); byte[] bytes = new byte[IPPREFIX.getFixedSize()]; if (address.length == 4) { if (subnetSize < 0 || 32 < subnetSize) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } for (int i = 0; i < 4; i++) { address[3 - i] &= -0x1 << min(max((32 - subnetSize) - 8 * i, 0), 8); } bytes[10] = (byte) 0xff; bytes[11] = (byte) 0xff; arraycopy(address, 0, bytes, 12, 4); bytes[IPPREFIX.getFixedSize() - 1] = (byte) subnetSize; } else if (address.length == 16) { if (subnetSize < 0 || 128 < subnetSize) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } for (int i = 0; i < 16; i++) { address[15 - i] &= (byte) -0x1 << min(max((128 - subnetSize) - 8 * i, 0), 8); } arraycopy(address, 0, bytes, 0, 16); bytes[IPPREFIX.getFixedSize() - 1] = (byte) subnetSize; } else { <START> throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid InetAddrss length: " + address.length); <END> } return wrappedBuffer(bytes); }</code><technical_language>this INVALID_CAST_ARGUMENT other more specific error code</technical_language>

Target:
public static Slice castFromVarcharToIpPrefix(@SqlType("varchar(x)") Slice slice) { byte[] address; int subnetSize; String string = slice.toStringUtf8(); if (!string.contains("/")) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } String[] parts = string.split("/"); try { address = InetAddresses.forString(parts[0]).getAddress(); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } subnetSize = Integer.parseInt(parts[1]); byte[] bytes = new byte[IPPREFIX.getFixedSize()]; if (address.length == 4) { if (subnetSize < 0 || 32 < subnetSize) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } for (int i = 0; i < 4; i++) { address[3 - i] &= -0x1 << min(max((32 - subnetSize) - 8 * i, 0), 8); } bytes[10] = (byte) 0xff; bytes[11] = (byte) 0xff; arraycopy(address, 0, bytes, 12, 4); bytes[IPPREFIX.getFixedSize() - 1] = (byte) subnetSize; } else if (address.length == 16) { if (subnetSize < 0 || 128 < subnetSize) { throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8()); } for (int i = 0; i < 16; i++) { address[15 - i] &= (byte) -0x1 << min(max((128 - subnetSize) - 8 * i, 0), 8); } arraycopy(address, 0, bytes, 0, 16); bytes[IPPREFIX.getFixedSize() - 1] = (byte) subnetSize; } else { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid InetAddress length: " + address.length); } return wrappedBuffer(bytes); }