public bool TryQueryScalarConfig(bool logErrors, out ServerScalarConfig serverScalarConfig, out HttpStatusCode?httpStatus, out string errorMessage)
        {
            Uri scalarConfigEndpoint;

            if (!this.TryCreateRepoEndpointUri(this.repoUrl, ScalarConstants.Endpoints.ScalarConfig, out scalarConfigEndpoint, out errorMessage))
            {
                serverScalarConfig = null;
                httpStatus         = null;
                return(false);
            }

            long requestId = HttpRequestor.GetNewRequestId();
            RetryWrapper <ServerScalarConfig> retrier = new RetryWrapper <ServerScalarConfig>(this.RetryConfig.MaxAttempts, CancellationToken.None);

            if (logErrors)
            {
                retrier.OnFailure += RetryWrapper <ServerScalarConfig> .StandardErrorHandler(this.Tracer, requestId, "QueryGvfsConfig");
            }

            RetryWrapper <ServerScalarConfig> .InvocationResult output = retrier.Invoke(
                tryCount =>
            {
                using (GitEndPointResponseData response = this.SendRequest(
                           requestId,
                           scalarConfigEndpoint,
                           HttpMethod.Get,
                           requestContent: null,
                           cancellationToken: CancellationToken.None))
                {
                    if (response.HasErrors)
                    {
                        return(new RetryWrapper <ServerScalarConfig> .CallbackResult(response.Error, response.ShouldRetry));
                    }

                    try
                    {
                        string configString       = response.RetryableReadToEnd();
                        ServerScalarConfig config = JsonConvert.DeserializeObject <ServerScalarConfig>(configString);
                        return(new RetryWrapper <ServerScalarConfig> .CallbackResult(config));
                    }
                    catch (JsonReaderException e)
                    {
                        return(new RetryWrapper <ServerScalarConfig> .CallbackResult(e, shouldRetry: false));
                    }
                }
            });

            if (output.Succeeded)
            {
                serverScalarConfig = output.Result;
                httpStatus         = HttpStatusCode.OK;
                return(true);
            }

            httpStatus = null;
            GitObjectsHttpException httpException = output.Error as GitObjectsHttpException;

            if (httpException != null)
            {
                httpStatus = httpException.StatusCode;
            }

            errorMessage = output.Error.Message;

            if (logErrors)
            {
                this.Tracer.RelatedError(
                    new EventMetadata
                {
                    { "Exception", output.Error.ToString() }
                },
                    $"{nameof(this.TryQueryScalarConfig)} failed");
            }

            serverScalarConfig = null;
            return(false);
        }
Exemple #2
0
        public bool TryQueryRepoInfo(bool logErrors, out VstsInfoData vstsInfo, out string errorMessage)
        {
            Uri repoInfoEndpoint;

            if (!this.TryCreateRepoEndpointUri(this.repoUrl, ScalarConstants.Endpoints.RepoInfo, out repoInfoEndpoint, out errorMessage))
            {
                vstsInfo = null;
                return(false);
            }

            long requestId = HttpRequestor.GetNewRequestId();
            RetryWrapper <VstsInfoData> retrier = new RetryWrapper <VstsInfoData>(this.RetryConfig.MaxAttempts, CancellationToken.None);

            retrier.OnFailure += RetryWrapper <VstsInfoData> .StandardErrorHandler(
                this.Tracer,
                requestId,
                "QueryVstsInfo",
                forceLogAsWarning : true); // Not all servers support /vsts/info

            RetryWrapper <VstsInfoData> .InvocationResult output = retrier.Invoke(
                tryCount =>
            {
                using (GitEndPointResponseData response = this.SendRequest(
                           requestId,
                           repoInfoEndpoint,
                           HttpMethod.Get,
                           requestContent: null,
                           cancellationToken: CancellationToken.None))
                {
                    if (response.HasErrors)
                    {
                        return(new RetryWrapper <VstsInfoData> .CallbackResult(response.Error, response.ShouldRetry));
                    }

                    try
                    {
                        string configString       = response.RetryableReadToEnd();
                        VstsInfoData vstsInfoData = JsonConvert.DeserializeObject <VstsInfoData>(
                            configString,
                            new JsonSerializerSettings
                        {
                            MissingMemberHandling = MissingMemberHandling.Ignore
                        });
                        return(new RetryWrapper <VstsInfoData> .CallbackResult(vstsInfoData));
                    }
                    catch (JsonReaderException e)
                    {
                        return(new RetryWrapper <VstsInfoData> .CallbackResult(e, shouldRetry: false));
                    }
                }
            });

            if (output.Succeeded)
            {
                vstsInfo     = output.Result;
                errorMessage = null;
                return(true);
            }

            GitObjectsHttpException httpException  = output.Error as GitObjectsHttpException;
            HttpStatusCode?         httpStatusCode = httpException?.StatusCode;

            vstsInfo = null;

            EventMetadata metadata = new EventMetadata();

            metadata.Add(nameof(httpStatusCode), httpStatusCode.ToString());
            metadata.Add(nameof(this.IsAnonymous), this.IsAnonymous);

            if (httpStatusCode == HttpStatusCode.NotFound ||
                (httpStatusCode == HttpStatusCode.Unauthorized && this.IsAnonymous))
            {
                errorMessage = null;
                this.Tracer.RelatedEvent(
                    EventLevel.Informational,
                    $"{nameof(this.TryQueryRepoInfo)}_NoVstsInfo",
                    metadata);

                // These failures are OK because not all servers support /vsts/info
                return(true);
            }

            metadata.Add("Exception", output.Error.ToString());
            this.Tracer.RelatedError(metadata, $"{nameof(this.TryQueryRepoInfo)} failed");

            errorMessage = output.Error.Message;
            return(false);
        }