private static <T> Callback<T> chainCallbackInternal(Callback<T> finalCallback, Object preAction) {
    return (result, exception) -> {
      try {
        if (exception == null && preAction != null) {
          if (preAction instanceof ThrowingFunction) {
            result = ((ThrowingFunction<T, T>) preAction).apply(result);
          } else if (preAction instanceof ThrowingConsumer) {
            ((ThrowingConsumer<T>) preAction).accept(result);
          } else {
            throw new RestServiceException("Unknown preAction type " + preAction.getClass().getName(),
                RestServiceErrorCode.InternalServerError);
          }
        }
      }
      catch (Throwable t) {
        exception = t instanceof Exception ? (Exception) t
            : new RestServiceException(t, RestServiceErrorCode.InternalServerError);
      }
      finally {
        // Note: we don't call the finalCallback in success case
        if (exception != null) {
          finalCallback.onCompletion(null, exception);
        }
      }
    };
  }
Create a {@link Callback} that invokes the preAction and then the callback with error handling.
It is meant to be a part of multi-step callback chain and handle intermediate failures in a consistent way.
@param <T> the input type for the callback accepts.
@param callback the callback to call after the preAction.
@param preAction the optional action to take before the callback is called.
@return the managed {@link Callback}.
ambry-api/src/main/java/com/github/ambry/commons/CallbackUtils.java