private Result resolveToFoundResult(PluginRequest request) {
    Result result = new Result(request);
    try {
      pluginResolver.resolve(request, result);
    } catch (Exception e) {
      throw new LocationAwareException(
          new GradleException(
              String.format("Error resolving plugin %s.", request.getDisplayName()), e),
          request.getScriptSource(),
          request.getLineNumber());
    }

    if (!result.isFound()) {
      String message = buildNotFoundMessage(request, result);
      Exception exception = new UnknownPluginException(message);
      throw new LocationAwareException(
          exception, request.getScriptSource(), request.getLineNumber());
    }

    return result;
  }
 private void applyPlugin(PluginRequest request, String id, Runnable applicator) {
   try {
     try {
       applicator.run();
     } catch (UnknownPluginException e) {
       throw new InvalidPluginException(
           String.format(
               "Could not apply requested plugin %s as it does not provide a plugin with id '%s'."
                   + " This is caused by an incorrect plugin implementation."
                   + " Please contact the plugin author(s).",
               request, id),
           e);
     } catch (Exception e) {
       throw new InvalidPluginException(
           String.format("An exception occurred applying plugin request %s", request), e);
     }
   } catch (Exception e) {
     throw new LocationAwareException(e, request.getScriptSource(), request.getLineNumber());
   }
 }