github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/error/error.go (about)

     1  /*
     2  Copyright 2013 CoreOS Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package error
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"net/http"
    23  )
    24  
    25  var errors = map[int]string{
    26  	// command related errors
    27  	EcodeKeyNotFound:    "Key not found",
    28  	EcodeTestFailed:     "Compare failed", //test and set
    29  	EcodeNotFile:        "Not a file",
    30  	EcodeNoMorePeer:     "Reached the max number of peers in the cluster",
    31  	EcodeNotDir:         "Not a directory",
    32  	EcodeNodeExist:      "Key already exists", // create
    33  	EcodeRootROnly:      "Root is read only",
    34  	EcodeKeyIsPreserved: "The prefix of given key is a keyword in etcd",
    35  	EcodeDirNotEmpty:    "Directory not empty",
    36  
    37  	// Post form related errors
    38  	EcodeValueRequired:        "Value is Required in POST form",
    39  	EcodePrevValueRequired:    "PrevValue is Required in POST form",
    40  	EcodeTTLNaN:               "The given TTL in POST form is not a number",
    41  	EcodeIndexNaN:             "The given index in POST form is not a number",
    42  	EcodeValueOrTTLRequired:   "Value or TTL is required in POST form",
    43  	EcodeTimeoutNaN:           "The given timeout in POST form is not a number",
    44  	EcodeNameRequired:         "Name is required in POST form",
    45  	EcodeIndexOrValueRequired: "Index or value is required",
    46  	EcodeIndexValueMutex:      "Index and value cannot both be specified",
    47  	EcodeInvalidField:         "Invalid field",
    48  
    49  	// raft related errors
    50  	EcodeRaftInternal: "Raft Internal Error",
    51  	EcodeLeaderElect:  "During Leader Election",
    52  
    53  	// etcd related errors
    54  	EcodeWatcherCleared:      "watcher is cleared due to etcd recovery",
    55  	EcodeEventIndexCleared:   "The event in requested index is outdated and cleared",
    56  	EcodeStandbyInternal:     "Standby Internal Error",
    57  	EcodeInvalidActiveSize:   "Invalid active size",
    58  	EcodeInvalidPromoteDelay: "Standby promote delay",
    59  	EcodePromoteError:        "Standby promotion error",
    60  }
    61  
    62  const (
    63  	EcodeKeyNotFound    = 100
    64  	EcodeTestFailed     = 101
    65  	EcodeNotFile        = 102
    66  	EcodeNoMorePeer     = 103
    67  	EcodeNotDir         = 104
    68  	EcodeNodeExist      = 105
    69  	EcodeKeyIsPreserved = 106
    70  	EcodeRootROnly      = 107
    71  	EcodeDirNotEmpty    = 108
    72  
    73  	EcodeValueRequired        = 200
    74  	EcodePrevValueRequired    = 201
    75  	EcodeTTLNaN               = 202
    76  	EcodeIndexNaN             = 203
    77  	EcodeValueOrTTLRequired   = 204
    78  	EcodeTimeoutNaN           = 205
    79  	EcodeNameRequired         = 206
    80  	EcodeIndexOrValueRequired = 207
    81  	EcodeIndexValueMutex      = 208
    82  	EcodeInvalidField         = 209
    83  
    84  	EcodeRaftInternal = 300
    85  	EcodeLeaderElect  = 301
    86  
    87  	EcodeWatcherCleared      = 400
    88  	EcodeEventIndexCleared   = 401
    89  	EcodeStandbyInternal     = 402
    90  	EcodeInvalidActiveSize   = 403
    91  	EcodeInvalidPromoteDelay = 404
    92  	EcodePromoteError        = 405
    93  )
    94  
    95  type Error struct {
    96  	ErrorCode int    `json:"errorCode"`
    97  	Message   string `json:"message"`
    98  	Cause     string `json:"cause,omitempty"`
    99  	Index     uint64 `json:"index"`
   100  }
   101  
   102  func NewError(errorCode int, cause string, index uint64) *Error {
   103  	return &Error{
   104  		ErrorCode: errorCode,
   105  		Message:   errors[errorCode],
   106  		Cause:     cause,
   107  		Index:     index,
   108  	}
   109  }
   110  
   111  func Message(code int) string {
   112  	return errors[code]
   113  }
   114  
   115  // Only for error interface
   116  func (e Error) Error() string {
   117  	return e.Message
   118  }
   119  
   120  func (e Error) toJsonString() string {
   121  	b, _ := json.Marshal(e)
   122  	return string(b)
   123  }
   124  
   125  func (e Error) Write(w http.ResponseWriter) {
   126  	w.Header().Add("X-Etcd-Index", fmt.Sprint(e.Index))
   127  	// 3xx is raft internal error
   128  	status := http.StatusBadRequest
   129  	switch e.ErrorCode {
   130  	case EcodeKeyNotFound:
   131  		status = http.StatusNotFound
   132  	case EcodeNotFile, EcodeDirNotEmpty:
   133  		status = http.StatusForbidden
   134  	case EcodeTestFailed, EcodeNodeExist:
   135  		status = http.StatusPreconditionFailed
   136  	default:
   137  		if e.ErrorCode/100 == 3 {
   138  			status = http.StatusInternalServerError
   139  		}
   140  	}
   141  	http.Error(w, e.toJsonString(), status)
   142  }