Пример #1
0
        /// <summary>
        /// Parses the response message and presents errors if neccessary.
        /// </summary>
        /// <param name="responseMessage">The response from the server.</param>
        /// <returns>The response message.</returns>
        private async Task <ControllerResponseDTO <T> > ParseResponse <T>(HttpResponseMessage responseMessage)
        {
            if (responseMessage == null)
            {
                throw new ArgumentNullException(nameof(responseMessage));
            }

            if (!responseMessage.IsSuccessStatusCode)
            {
                // if we fail here fire an unknown sweet alert and return with an empty controller response.
                await SweetAlertService.FireAsync(AlertHelper.UnknownError());

                return(new ControllerResponseDTO <T>());
            }

            var json = await responseMessage.Content.ReadAsStringAsync();

            var controllerResponse = JsonConvert.DeserializeObject <ControllerResponseDTO <T> >(json);

            if (controllerResponse.HasError)
            {
                await SweetAlertService.FireAsync(AlertHelper.ValidationError(controllerResponse.ErrorMessage));
            }

            //todo: consider throwing custom errors here once solution becomes more robust
            return(controllerResponse);
        }