public static BaseResponseRestModel HandleException(Exception exception) { Debug.WriteLine(exception); var response = new BaseResponseRestModel() { Message = "Exception" }; if (exception.GetType() == typeof(TaskCanceledException)) { response.Message = "Timeout - Server not responding..."; } else if (exception is Refit.ApiException apiException) { switch (apiException.StatusCode) { case HttpStatusCode.Forbidden: response.Message = "Forbidden"; break; default: response.Message = "Server Error\n" + apiException.Message; break; } } else if (exception.GetType() == typeof(WebException) || (exception.InnerException != null && exception.InnerException.GetType() == typeof(WebException))) { response.Message = "Can not connect"; } else { if (string.IsNullOrEmpty(exception.Data.ToString())) { response.Message = "Internal error"; } else { response.Message = String.IsNullOrEmpty(exception.Message) ? exception.Data.ToString() : exception.Message; } } return(response); }
public async Task <BaseResponseRestModel> PerformNetworkRequest(TaskType type, Dictionary <string, object> args, bool showIndicator) { BaseResponseRestModel answer = null; IApiResponse apiResponse = null; try { if (Connectivity.NetworkAccess != NetworkAccess.Internet) { throw new InternalException("Network is not available"); } if (!Initialized) { Init(BASE_URL); } if (showIndicator) { loadingIndicatorManager.ShowIndicator(); } switch (type) { case TaskType.LOGIN: { if (args.TryGetValue("arg1", out object reqObj) && reqObj is LoginRequest request) { var response = await api.Login(request, Constants.REQUEST_PARAM_DEVELOPER); apiResponse = response; answer = response.Content; } break; } case TaskType.GET_TASKS: { if (args.TryGetValue("arg1", out object reqObj) && reqObj is GetTasksRequest request) { var response = await api.GetTasks(request, Constants.REQUEST_PARAM_DEVELOPER); apiResponse = response; answer = response.Content; } break; } case TaskType.CREATE_TASK: { if (args.TryGetValue("arg1", out object reqObj) && reqObj is CreateTaskRequest request) { var response = await api.CreateTask(request, Constants.REQUEST_PARAM_DEVELOPER); apiResponse = response; answer = response.Content; } break; } case TaskType.EDIT_TASK: { if (args.TryGetValue("arg1", out object reqObj) && reqObj is EditTaskRequest request && args.TryGetValue("arg2", out object idObj) && idObj is string taskId) { request.Token = activeToken; var response = await api.EditTask(request, taskId, Constants.REQUEST_PARAM_DEVELOPER); apiResponse = response; answer = response.Content; } break; } } if (showIndicator) { loadingIndicatorManager.HideIndicator(); } if (answer == null) { throw new InternalException(apiResponse != null ? apiResponse.Error.Content : String.Format("Unknown arguments for {0} request", type.ToString())); } if (answer.IsStatusOk) { apiResponse?.Dispose(); return(answer); } if (answer.Data is JObject dict) { List <string> list = new List <string>(); foreach (JToken token in dict.Children()) { if (token is JProperty) { var prop = token as JProperty; list.Add(prop.Value.ToString()); } } throw new InternalException(String.Join("\n", list)); } else { throw new InternalException(answer.Data as string); } } catch (Exception e) { apiResponse?.Dispose(); loadingIndicatorManager.HideIndicator(); var exp = ErrorHandler.HandleException(e); MessagingCenter.Send <string>(exp != null ? exp.Message : e.Message, Constants.ERROR_OCCURED); return(null); } }