OpenRA.Download.FormatErrorMessage C# (CSharp) Method

FormatErrorMessage() public static method

public static FormatErrorMessage ( Exception e ) : string
e System.Exception
return string
        public static string FormatErrorMessage(Exception e)
        {
            var ex = e as WebException;
            if (ex == null)
                return e.Message;

            switch (ex.Status)
            {
                case WebExceptionStatus.RequestCanceled:
                    return "Cancelled";
                case WebExceptionStatus.NameResolutionFailure:
                    return "DNS lookup failed";
                case WebExceptionStatus.Timeout:
                    return "Connection timeout";
                case WebExceptionStatus.ConnectFailure:
                    return "Cannot connect to remote server";
                case WebExceptionStatus.ProtocolError:
                    return "File not found on remote server";
                default:
                    return ex.Message;
            }
        }

Usage Example

Example #1
0
        public void QueryRemoteMapDetails(string repositoryUrl, IEnumerable <string> uids, Action <MapPreview> mapDetailsReceived = null, Action queryFailed = null)
        {
            var maps = uids.Distinct()
                       .Select(uid => previews[uid])
                       .Where(p => p.Status == MapStatus.Unavailable)
                       .ToDictionary(p => p.Uid, p => p);

            if (!maps.Any())
            {
                return;
            }

            foreach (var p in maps.Values)
            {
                p.UpdateRemoteSearch(MapStatus.Searching, null);
            }

            var url = repositoryUrl + "hash/" + string.Join(",", maps.Keys) + "/yaml";

            Action <DownloadDataCompletedEventArgs> onInfoComplete = i =>
            {
                if (i.Error != null)
                {
                    Log.Write("debug", "Remote map query failed with error: {0}", Download.FormatErrorMessage(i.Error));
                    Log.Write("debug", "URL was: {0}", url);
                    foreach (var p in maps.Values)
                    {
                        p.UpdateRemoteSearch(MapStatus.Unavailable, null);
                    }

                    if (queryFailed != null)
                    {
                        queryFailed();
                    }

                    return;
                }

                var data = Encoding.UTF8.GetString(i.Result);
                try
                {
                    var yaml = MiniYaml.FromString(data);
                    foreach (var kv in yaml)
                    {
                        maps[kv.Key].UpdateRemoteSearch(MapStatus.DownloadAvailable, kv.Value, mapDetailsReceived);
                    }
                }
                catch (Exception e)
                {
                    Log.Write("debug", "Can't parse remote map search data:\n{0}", data);
                    Log.Write("debug", "Exception: {0}", e);
                    if (queryFailed != null)
                    {
                        queryFailed();
                    }
                }
            };

            new Download(url, _ => { }, onInfoComplete);
        }
All Usage Examples Of OpenRA.Download::FormatErrorMessage