Beispiel #1
0
        public async Task <Response> QueryAsync(Type type, string payload)
        {
            if (!token.HasValue)
            {
                token.Value = await interop.LoadTokenAsync();

                EnsureAuthorization();
            }

            string url = queryMapper.FindUrlByType(type);

            if (url != null)
            {
                try
                {
                    HttpResponseMessage response = await http.PostAsync($"/api/query/{url}", new StringContent(payload, Encoding.UTF8, "text/json"));

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string responseContent = await response.Content.ReadAsStringAsync();

                        log.Debug($"Response: '{responseContent}'.");
                        return(json.Deserialize <Response>(responseContent));
                    }
                    else if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        ClearAuthorization();
                        throw new UnauthorizedAccessException();
                    }
                    else if (response.StatusCode == HttpStatusCode.InternalServerError)
                    {
                        throw new InternalServerException();
                    }
                    else
                    {
                        throw Ensure.Exception.InvalidOperation($"Generic HTTP error: {response.StatusCode}.");
                    }
                }
                catch (Exception e)
                {
                    if (e is HttpRequestException)
                    {
                        e = new ServerNotRespondingException(e);
                    }

                    exceptionHandler.Handle(e);
                    throw;
                }
            }
            else
            {
                return(await http.PostJsonAsync <Response>($"/api/query", CreateRequest(type, payload)));
            }
        }
Beispiel #2
0
        private Exception ProcessHttpException(Exception e)
        {
            log.Debug($"Exception while invoking HTTP request, type: '{e.GetType().FullName}', message: '{e.Message}'.");

            if (e.Message == "TypeError: Failed to fetch" || e is HttpRequestException)
            {
                e = new ServerNotRespondingException(e);
            }

            exceptionHandler.Handle(e);
            return(e);
        }
 private void OnServerNotRespondingException(ServerNotRespondingException e)
 {
     log.Debug($"Catch '{nameof(ServerNotRespondingException)}'.");
     isServerException = true;
     RaiseChanged();
 }