示例#1
0
        private async Task EnsureSuccessResponseAsync(HttpResponseMessage responseMessage)
        {
            log.Debug($"Api headers: '{responseMessage.Headers}'.");

            if (responseMessage.Headers.TryGetValues(VersionHeader.Name, out var versions))
            {
                var rawVersion = versions.FirstOrDefault();

                log.Debug($"ApiVersion is '{rawVersion}'.");

                if (!String.IsNullOrEmpty(rawVersion) && Version.TryParse(rawVersion, out var version))
                {
                    versionChecker.Ensure(version);
                }
            }

            if (responseMessage.StatusCode == HttpStatusCode.OK)
            {
                return;
            }
            else if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
            {
                await authenticationState.ClearTokenAsync();

                throw new UnauthorizedAccessException();
            }
            else if (responseMessage.StatusCode == HttpStatusCode.InternalServerError)
            {
                throw new InternalServerException();
            }
            else
            {
                throw Ensure.Exception.InvalidOperation($"Generic HTTP error: {responseMessage.StatusCode}.");
            }
        }
示例#2
0
        private async Task EnsureSuccessResponseAsync(HttpResponseMessage responseMessage)
        {
            log.Debug($"Api headers: '{responseMessage.Headers}'.");

            if (responseMessage.Headers.TryGetValues(VersionHeader.Name, out var versions))
            {
                var rawVersion = versions.FirstOrDefault();

                log.Debug($"ApiVersion is '{rawVersion}'.");

                if (!String.IsNullOrEmpty(rawVersion) && Version.TryParse(rawVersion, out var version))
                {
                    versionChecker.Ensure(version);
                }
            }

            if (responseMessage.StatusCode == HttpStatusCode.OK)
            {
                return;
            }
            else if (responseMessage.StatusCode == HttpStatusCode.Unauthorized)
            {
                await authenticationState.ClearTokenAsync();

                throw new UnauthorizedAccessException();
            }
            else if (responseMessage.StatusCode == HttpStatusCode.InternalServerError)
            {
                if (responseMessage.Content.Headers.ContentType.MediaType == "text/json")
                {
                    Response response = await ReadJsonAsync <Response>(responseMessage);

                    log.Debug($"Managed server exception '{response.Type}' with payload '{response.Payload}'.");

                    Type exceptionType = Type.GetType(response.Type);
                    if (exceptionType != null)
                    {
                        Exception serverException = (Exception)formatters.Exception.Deserialize(exceptionType, response.Payload);
                        throw serverException;
                    }
                }

                throw new InternalServerException();
            }
            else
            {
                throw Ensure.Exception.InvalidOperation($"Generic HTTP error: {responseMessage.StatusCode}.");
            }
        }