error.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "encoding/json"
  4. )
  5. var errors map[int]string
  6. func init() {
  7. errors = make(map[int]string)
  8. // command related errors
  9. errors[100] = "Key Not Found"
  10. errors[101] = "The given PrevValue is not equal to the value of the key"
  11. errors[102] = "Not A File"
  12. errors[103] = "Reached the max number of machines in the cluster"
  13. // Post form related errors
  14. errors[200] = "Value is Required in POST form"
  15. errors[201] = "PrevValue is Required in POST form"
  16. errors[202] = "The given TTL in POST form is not a number"
  17. errors[203] = "The given index in POST form is not a number"
  18. // raft related errors
  19. errors[300] = "Raft Internal Error"
  20. errors[301] = "During Leader Election"
  21. // keyword
  22. errors[400] = "The prefix of the given key is a keyword in etcd"
  23. // etcd related errors
  24. errors[500] = "watcher is cleared due to etcd recovery"
  25. }
  26. type jsonError struct {
  27. ErrorCode int `json:"errorCode"`
  28. Message string `json:"message"`
  29. Cause string `json:"cause,omitempty"`
  30. }
  31. func newJsonError(errorCode int, cause string) []byte {
  32. b, _ := json.Marshal(jsonError{
  33. ErrorCode: errorCode,
  34. Message: errors[errorCode],
  35. Cause: cause,
  36. })
  37. return b
  38. }