Represents the generic exception from etcd https://github.com/coreos/etcd/blob/master/Documentation/errorcode.md EtcdGenericException ├── EtcdCommonException | ├─ KeyNotFound | ├─ TestFailed | ├─ NotFile | ├─ NotDir | ├─ NodeExist | ├─ RootReadOnly | └─ DirNotEmpty ├── EtcdPostFormException | ├─ PrevValueRequired | ├─ TTLNaN | ├─ IndexNaN | ├─ InvalidField | └─ InvalidForm ├── EtcdRaftException | ├─ RaftInternal | └─ LeaderElect └── EtcdException ├─ WatcherCleared └─ EventIndexCleared
Inheritance: System.Exception
Exemple #1
0
        internal static EtcdGenericException Create(HttpRequestMessage request, ErrorResponse errorResponse)
        {
            int code = errorResponse.ErrorCode;

            string message = string.Format( CultureInfo.InvariantCulture
                , "{0} {1} failed. {2}; Code={3}; Cause=`{4}`; "
                , request.Method.ToString().ToUpperInvariant()
                , request.RequestUri
                , errorResponse.Message
                , errorResponse.ErrorCode
                , errorResponse.Cause
                );

            EtcdGenericException exception;
            if (code >= 100 && code <= 199)
                exception = EtcdCommonException.Create(code, message);
            else if (code >= 200 && code <= 299)
                exception = EtcdPostFormException.Create(code, message);
            else if (code >= 300 && code <= 399)
                exception = EtcdRaftException.Create(code, message);
            else if (code >= 400 && code <= 499)
                exception = EtcdException.Create(code, message);
            else
                exception = new EtcdGenericException(message);

            exception.Code = (ErrorCode)code;
            exception.Cause = errorResponse.Cause;
            return exception;
        }
Exemple #2
0
        internal static EtcdGenericException Create(HttpRequestMessage request, ErrorResponse errorResponse)
        {
            int code = errorResponse.ErrorCode;

            string message = string.Format(CultureInfo.InvariantCulture
                                           , "{0} {1} failed. {2}; Code={3}; Cause=`{4}`; "
                                           , request.Method.ToString().ToUpperInvariant()
                                           , request.RequestUri
                                           , errorResponse.Message
                                           , errorResponse.ErrorCode
                                           , errorResponse.Cause
                                           );

            EtcdGenericException exception;

            if (code >= 100 && code <= 199)
            {
                exception = EtcdCommonException.Create(code, message);
            }
            else if (code >= 200 && code <= 299)
            {
                exception = EtcdPostFormException.Create(code, message);
            }
            else if (code >= 300 && code <= 399)
            {
                exception = EtcdRaftException.Create(code, message);
            }
            else if (code >= 400 && code <= 499)
            {
                exception = EtcdException.Create(code, message);
            }
            else
            {
                exception = new EtcdGenericException(message);
            }

            exception.Code  = (ErrorCode)code;
            exception.Cause = errorResponse.Cause;
            return(exception);
        }
Exemple #3
0
        private async Task <EtcdResponse> SendRequest(HttpMethod method, string requestUri, IEnumerable <KeyValuePair <string, string> > formFields = null)
        {
            HttpClientEx startClient   = _currentClient;
            HttpClientEx currentClient = _currentClient;

            for (; ;)
            {
                try
                {
                    using (HttpRequestMessage requestMessage = new HttpRequestMessage(method, requestUri))
                    {
                        if (formFields != null)
                        {
                            requestMessage.Content = new FormUrlEncodedContent(formFields);
                        }

                        using (HttpResponseMessage responseMessage = await currentClient.SendAsync(requestMessage))
                        {
                            string json = null;

                            if (responseMessage.Content != null)
                            {
                                json = await responseMessage.Content.ReadAsStringAsync();
                            }

                            if (!responseMessage.IsSuccessStatusCode)
                            {
                                if (!string.IsNullOrWhiteSpace(json))
                                {
                                    ErrorResponse errorResponse = null;
                                    try
                                    {
                                        errorResponse = _jsonDeserializer.Deserialize <ErrorResponse>(json);
                                    }
                                    catch { }

                                    if (errorResponse != null)
                                    {
                                        throw EtcdGenericException.Create(requestMessage, errorResponse);
                                    }
                                }


                                currentClient = currentClient.Next;
                                if (currentClient != startClient)
                                {
                                    // try the next
                                    continue;
                                }
                                else
                                {
                                    responseMessage.EnsureSuccessStatusCode();
                                }
                            }

                            // if currentClient != _currentClient, update _currentClient
                            if (currentClient != startClient)
                            {
                                Interlocked.CompareExchange(ref _currentClient, currentClient, startClient);
                            }

                            if (!string.IsNullOrWhiteSpace(json))
                            {
                                EtcdResponse resp = _jsonDeserializer.Deserialize <EtcdResponse>(json);
                                resp.EtcdServer    = currentClient.BaseAddress.OriginalString;
                                resp.EtcdClusterID = GetStringHeader(responseMessage, "X-Etcd-Cluster-Id");
                                resp.EtcdIndex     = GetLongHeader(responseMessage, "X-Etcd-Index");
                                resp.RaftIndex     = GetLongHeader(responseMessage, "X-Raft-Index");
                                resp.RaftTerm      = GetLongHeader(responseMessage, "X-Raft-Term");

                                long previousIndex = _lastIndex;
                                if (resp.EtcdIndex > previousIndex)
                                {
                                    Interlocked.CompareExchange(ref _lastIndex, resp.EtcdIndex, previousIndex);
                                }
                                this.ClusterID = resp.EtcdClusterID;
                                return(resp);
                            }
                            return(null);
                        }
                    }
                }
                catch (EtcdRaftException)
                {
                    currentClient = currentClient.Next;
                    if (currentClient != startClient)
                    {
                        continue; // try the next
                    }
                    else
                    {
                        throw; // tried all clients, all failed
                    }
                }
                catch (EtcdGenericException)
                {
                    throw;
                }
                catch (Exception)
                {
                    currentClient = currentClient.Next;
                    if (currentClient != startClient)
                    {
                        continue; // try the next
                    }
                    else
                    {
                        throw; // tried all clients, all failed
                    }
                }
            }
        }