static void replaceOperationException(AtomicReference<Exception> operationExceptionRef, RouterException exception,
      ToIntFunction<RouterErrorCode> precedenceLevelFn) {
    operationExceptionRef.updateAndGet(currentException -> {
      Exception newException;
      if (currentException == null) {
        newException = exception;
      } else {
        int currentPrecedence = precedenceLevelFn.applyAsInt(
            currentException instanceof RouterException ? ((RouterException) currentException).getErrorCode()
                : RouterErrorCode.UnexpectedInternalError);
        newException =
            precedenceLevelFn.applyAsInt(exception.getErrorCode()) < currentPrecedence ? exception : currentException;
      }
      return newException;
    });
  }
Atomically replace the exception for an operation depending on the precedence of the new exception.
First, if the current operationException is null, directly set operationException as exception;
Second, if operationException exists, compare ErrorCodes of exception and existing operation Exception depending
on precedence level. An ErrorCode with a smaller precedence level overrides an ErrorCode with a larger precedence
level. Update the operationException if necessary.
@param operationExceptionRef the {@link AtomicReference} to the operation exception to potentially replace.
@param exception the new {@link RouterException} to set if it the precedence level is lower than that of the
                 current exception.
@param precedenceLevelFn a function that translates a {@link RouterErrorCode} into an integer precedence level,
                         where lower values signify greater precedence.
ambry-router/src/main/java/com/github/ambry/router/RouterUtils.java