public ErrorMessage decode()

in src/java/org/apache/cassandra/transport/messages/ErrorMessage.java [50:193]


        public ErrorMessage decode(ByteBuf body, ProtocolVersion version)
        {
            ExceptionCode code = ExceptionCode.fromValue(body.readInt());
            String msg = CBUtil.readString(body);

            TransportException te = null;
            switch (code)
            {
                case SERVER_ERROR:
                    te = new ServerError(msg);
                    break;
                case PROTOCOL_ERROR:
                    te = new ProtocolException(msg);
                    break;
                case BAD_CREDENTIALS:
                    te = new AuthenticationException(msg);
                    break;
                case UNAVAILABLE:
                    {
                        ConsistencyLevel cl = CBUtil.readConsistencyLevel(body);
                        int required = body.readInt();
                        int alive = body.readInt();
                        te = UnavailableException.create(cl, required, alive);
                    }
                    break;
                case OVERLOADED:
                    te = new OverloadedException(msg);
                    break;
                case IS_BOOTSTRAPPING:
                    te = new IsBootstrappingException();
                    break;
                case TRUNCATE_ERROR:
                    te = new TruncateException(msg);
                    break;
                case WRITE_FAILURE:
                case READ_FAILURE:
                    {
                        ConsistencyLevel cl = CBUtil.readConsistencyLevel(body);
                        int received = body.readInt();
                        int blockFor = body.readInt();
                        // The number of failures is also present in protocol v5, but used instead to specify the size of the failure map
                        int failure = body.readInt();

                        Map<InetAddressAndPort, RequestFailureReason> failureReasonByEndpoint;
                        if (version.isGreaterOrEqualTo(ProtocolVersion.V5))
                        {
                            ImmutableMap.Builder<InetAddressAndPort, RequestFailureReason> builder = ImmutableMap.builderWithExpectedSize(failure);
                            for (int i = 0; i < failure; i++)
                            {
                                InetAddress endpoint = CBUtil.readInetAddr(body);
                                RequestFailureReason failureReason = RequestFailureReason.fromCode(body.readUnsignedShort());
                                builder.put(InetAddressAndPort.getByAddress(endpoint), failureReason);
                            }
                            failureReasonByEndpoint = builder.build();
                        }
                        else
                        {
                            failureReasonByEndpoint = Collections.emptyMap();
                        }

                        if (code == ExceptionCode.WRITE_FAILURE)
                        {
                            WriteType writeType = Enum.valueOf(WriteType.class, CBUtil.readString(body));
                            te = new WriteFailureException(cl, received, blockFor, writeType, failureReasonByEndpoint);
                        }
                        else
                        {
                            byte dataPresent = body.readByte();
                            te = new ReadFailureException(cl, received, blockFor, dataPresent != 0, failureReasonByEndpoint);
                        }
                    }
                    break;
                case WRITE_TIMEOUT:
                case READ_TIMEOUT:
                    {
                        ConsistencyLevel cl = CBUtil.readConsistencyLevel(body);
                        int received = body.readInt();
                        int blockFor = body.readInt();
                        if (code == ExceptionCode.WRITE_TIMEOUT)
                        {
                            WriteType writeType = Enum.valueOf(WriteType.class, CBUtil.readString(body));
                            if (version.isGreaterOrEqualTo(ProtocolVersion.V5) && writeType == WriteType.CAS)
                            {
                                int contentions = body.readShort();
                                te = new CasWriteTimeoutException(writeType, cl, received, blockFor, contentions);
                            }
                            else
                            {
                                te = new WriteTimeoutException(writeType, cl, received, blockFor);
                            }
                        }
                        else
                        {
                            byte dataPresent = body.readByte();
                            te = new ReadTimeoutException(cl, received, blockFor, dataPresent != 0);
                        }
                        break;
                    }
                case FUNCTION_FAILURE:
                    String fKeyspace = CBUtil.readString(body);
                    String fName = CBUtil.readString(body);
                    List<String> argTypes = CBUtil.readStringList(body);
                    te = FunctionExecutionException.create(new FunctionName(fKeyspace, fName), argTypes, msg);
                    break;
                case UNPREPARED:
                    {
                        MD5Digest id = MD5Digest.wrap(CBUtil.readBytes(body));
                        te = new PreparedQueryNotFoundException(id);
                    }
                    break;
                case SYNTAX_ERROR:
                    te = new SyntaxException(msg);
                    break;
                case UNAUTHORIZED:
                    te = new UnauthorizedException(msg);
                    break;
                case INVALID:
                    te = new InvalidRequestException(msg);
                    break;
                case CONFIG_ERROR:
                    te = new ConfigurationException(msg);
                    break;
                case CDC_WRITE_FAILURE:
                    te = new CDCWriteException(msg);
                    break;
                case ALREADY_EXISTS:
                    String ksName = CBUtil.readString(body);
                    String cfName = CBUtil.readString(body);
                    if (cfName.isEmpty())
                        te = new AlreadyExistsException(ksName);
                    else
                        te = new AlreadyExistsException(ksName, cfName);
                    break;
                case CAS_WRITE_UNKNOWN:
                    assert version.isGreaterOrEqualTo(ProtocolVersion.V5);

                    ConsistencyLevel cl = CBUtil.readConsistencyLevel(body);
                    int received = body.readInt();
                    int blockFor = body.readInt();
                    te = new CasWriteUnknownResultException(cl, received, blockFor);
                    break;
            }
            return new ErrorMessage(te);
        }