error.go 886 B

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