private T processResponse()

in commit-status-publisher-server/src/main/java/jetbrains/buildServer/commitPublisher/github/api/impl/GitHubApiImpl.java [309:359]


  private <T> T processResponse(@NotNull String uri, @NotNull final Class<T> clazz, boolean logErrorsDebugOnly) throws IOException, PublisherException {
    LoggerUtil.logRequest(Constants.GITHUB_PUBLISHER_ID, HttpMethod.GET, uri, null);

    final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
    final AtomicReference<T> resultRef = new AtomicReference<>();
    IOGuard.allowNetworkCall(() -> {
      myClient.get(uri, authenticationCredentials(), defaultHeaders(),
                   success -> {
                     final String json = success.getBodyAsString();
                     if (StringUtil.isEmptyOrSpaces(json)) {
                       logFailedResponse(HttpMethod.GET, uri, null, success, logErrorsDebugOnly);
                       exceptionRef.set(new IOException(getErrorMessage(success, "Empty response.")));
                     } else {
                       LOG.debug("Parsing json for " + uri + ": " + json);
                       T result = myGson.fromJson(json, clazz);
                       if (null == result) {
                         exceptionRef.set(new PublisherException("GitHub publisher fails to parse a response"));
                       } else {
                         resultRef.set(result);
                       }
                     }
                   },
                   error -> {
                     String responseBody = logFailedResponse(HttpMethod.GET, uri, null, error, logErrorsDebugOnly);
                     String githubError = parseErrorsFromResponse(responseBody);
                     String additionalComment = githubError != null ? githubError :  error.getStatusCode() == HttpStatus.SC_NOT_FOUND ? MSG_NOT_FOUND : MSG_PROXY_OR_PERMISSIONS;
                     PublisherException ex = new PublisherException(getErrorMessage(error, additionalComment));
                     if (RetryResponseProcessor.shouldRetryOnCode(error.getStatusCode())) {
                       ex.setShouldRetry();
                     }
                     exceptionRef.set(ex);
                   },
                   e -> {
                     exceptionRef.set(e);
                   }
      );
    });

    final Exception ex;
    if ((ex = exceptionRef.get()) != null) {
      if (ex instanceof PublisherException) {
        throw (PublisherException)ex;
      } else {
        PublisherException e = new PublisherException(ex.getMessage(), ex);
        RetryResponseProcessor.processNetworkException(ex, e);
        throw e;
      }
    }

    return resultRef.get();
  }