error.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. }
  24. type jsonError struct {
  25. ErrorCode int `json:"errorCode"`
  26. Message string `json:"message"`
  27. Cause string `json:"cause,omitempty"`
  28. }
  29. func newJsonError(errorCode int, cause string) []byte {
  30. b, _ := json.Marshal(jsonError{
  31. ErrorCode: errorCode,
  32. Message: errors[errorCode],
  33. Cause: cause,
  34. })
  35. return b
  36. }