error.go 954 B

123456789101112131415161718192021222324252627282930313233343536373839
  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. // Post form related errors
  13. errors[200] = "Value is Required in POST form"
  14. errors[201] = "PrevValue is Required in POST form"
  15. errors[202] = "The given TTL in POST form is not a number"
  16. errors[203] = "The given index in POST form is not a number"
  17. // raft related errors
  18. errors[300] = "Raft Internal Error"
  19. errors[301] = "During Leader Election"
  20. }
  21. type jsonError struct {
  22. ErrorCode int `json:"errorCode"`
  23. Message string `json:"message"`
  24. Cause string `json:"cause,omitempty"`
  25. }
  26. func newJsonError(errorCode int, cause string) []byte {
  27. b, _ := json.Marshal(jsonError{
  28. ErrorCode: errorCode,
  29. Message: errors[errorCode],
  30. Cause: cause,
  31. })
  32. return b
  33. }