internal AVException(AVException.ErrorCode code, string message, Exception cause) : base(message, cause)
		{
			this.Code = code;
		}
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="command"></param>
        /// <param name="uploadProgress"></param>
        /// <param name="downloadProgress"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task <Tuple <HttpStatusCode, IDictionary <string, object> > > RunCommandAsync(AVCommand command,
                                                                                             IProgress <AVUploadProgressEventArgs> uploadProgress     = null,
                                                                                             IProgress <AVDownloadProgressEventArgs> downloadProgress = null,
                                                                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            return(PrepareCommand(command).ContinueWith(commandTask =>
            {
                var requestLog = commandTask.Result.ToLog();
                AVClient.PrintLog("http=>" + requestLog);

                return httpClient.ExecuteAsync(commandTask.Result, uploadProgress, downloadProgress, cancellationToken).OnSuccess(t =>
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    var response = t.Result;
                    var contentString = response.Item2;
                    int responseCode = (int)response.Item1;

                    var responseLog = responseCode + ";" + contentString;
                    AVClient.PrintLog("http<=" + responseLog);

                    if (responseCode >= 500)
                    {
                        // Server error, return InternalServerError.
                        throw new AVException(AVException.ErrorCode.InternalServerError, response.Item2);
                    }
                    else if (contentString != null)
                    {
                        IDictionary <string, object> contentJson = null;
                        try
                        {
                            if (contentString.StartsWith("["))
                            {
                                var arrayJson = Json.Parse(contentString);
                                contentJson = new Dictionary <string, object> {
                                    { "results", arrayJson }
                                };
                            }
                            else
                            {
                                contentJson = Json.Parse(contentString) as IDictionary <string, object>;
                            }
                        }
                        catch (Exception e)
                        {
                            throw new AVException(AVException.ErrorCode.OtherCause,
                                                  "Invalid response from server", e);
                        }
                        if (responseCode < 200 || responseCode > 299)
                        {
                            AVClient.PrintLog("error response code:" + responseCode);
                            int code = (int)(contentJson.ContainsKey("code") ? (int)contentJson["code"] : (int)AVException.ErrorCode.OtherCause);
                            string error = contentJson.ContainsKey("error") ?
                                           contentJson["error"] as string : contentString;
                            AVException.ErrorCode ec = (AVException.ErrorCode)code;
                            throw new AVException(ec, error);
                        }
                        return new Tuple <HttpStatusCode, IDictionary <string, object> >(response.Item1,
                                                                                         contentJson);
                    }
                    return new Tuple <HttpStatusCode, IDictionary <string, object> >(response.Item1, null);
                });
            }).Unwrap());
        }