private void maybeTransition(
        OptionalInt leaderId,
        int epoch,
        Endpoints leaderEndpoints,
        long currentTimeMs
    ) {
        if (!hasConsistentLeader(epoch, leaderId)) {
            throw new IllegalStateException("Received request or response with leader " + leaderId +
                " and epoch " + epoch + " which is inconsistent with current leader " +
                quorum.leaderId() + " and epoch " + quorum.epoch());
        } else if (epoch > quorum.epoch()) {
            if (leaderId.isPresent()) {
                transitionToFollower(epoch, leaderId.getAsInt(), leaderEndpoints, currentTimeMs);
            } else {
                transitionToUnattached(epoch);
            }
        } else if (
                leaderId.isPresent() &&
                (!quorum.hasLeader() || leaderEndpoints.size() > quorum.leaderEndpoints().size())
        ) {
            // The request or response indicates the leader of the current epoch
            // which are currently unknown or the replica has discovered more endpoints
            transitionToFollower(epoch, leaderId.getAsInt(), leaderEndpoints, currentTimeMs);
        }
    }
Handle response errors that are common across request types.

@param error Error from the received response
@param leaderId Optional leaderId from the response
@param epoch Epoch received from the response
@param leaderEndpoints the endpoints of the leader from the response
@param source the node that sent the response
@param currentTimeMs Current epoch time in milliseconds
@return Optional value indicating whether the error was handled here and the outcome of
   that handling. Specifically:

   - Optional.empty means that the response was not handled here and the custom
       API handler should be applied
   - Optional.of(true) indicates that the response was successfully handled here and
       the node can become ready
   - Optional.of(false) indicates that the response was handled here, but that the
       node should got in to backoff
raft/src/main/java/org/apache/kafka/raft/KafkaRaftClient.java