errors.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package validation
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. // VALIDATION ERRORS
  7. // ErrValidation represents a general validation error
  8. type ErrValidation struct {
  9. Msg string
  10. }
  11. func (err ErrValidation) Error() string {
  12. return fmt.Sprintf("An error occurred during validation: %s", err.Msg)
  13. }
  14. // ErrBadHierarchy represents missing metadata. Currently: a missing snapshot
  15. // at this current time. When delegations are implemented it will also
  16. // represent a missing delegation parent
  17. type ErrBadHierarchy struct {
  18. Missing string
  19. Msg string
  20. }
  21. func (err ErrBadHierarchy) Error() string {
  22. return fmt.Sprintf("Metadata hierarchy is incomplete: %s", err.Msg)
  23. }
  24. // ErrBadRoot represents a failure validating the root
  25. type ErrBadRoot struct {
  26. Msg string
  27. }
  28. func (err ErrBadRoot) Error() string {
  29. return fmt.Sprintf("The root metadata is invalid: %s", err.Msg)
  30. }
  31. // ErrBadTargets represents a failure to validate a targets (incl delegations)
  32. type ErrBadTargets struct {
  33. Msg string
  34. }
  35. func (err ErrBadTargets) Error() string {
  36. return fmt.Sprintf("The targets metadata is invalid: %s", err.Msg)
  37. }
  38. // ErrBadSnapshot represents a failure to validate the snapshot
  39. type ErrBadSnapshot struct {
  40. Msg string
  41. }
  42. func (err ErrBadSnapshot) Error() string {
  43. return fmt.Sprintf("The snapshot metadata is invalid: %s", err.Msg)
  44. }
  45. // END VALIDATION ERRORS
  46. // SerializableError is a struct that can be used to serialize an error as JSON
  47. type SerializableError struct {
  48. Name string
  49. Error error
  50. }
  51. // UnmarshalJSON attempts to unmarshal the error into the right type
  52. func (s *SerializableError) UnmarshalJSON(text []byte) (err error) {
  53. var x struct{ Name string }
  54. err = json.Unmarshal(text, &x)
  55. if err != nil {
  56. return
  57. }
  58. var theError error
  59. switch x.Name {
  60. case "ErrValidation":
  61. var e struct{ Error ErrValidation }
  62. err = json.Unmarshal(text, &e)
  63. theError = e.Error
  64. case "ErrBadHierarchy":
  65. var e struct{ Error ErrBadHierarchy }
  66. err = json.Unmarshal(text, &e)
  67. theError = e.Error
  68. case "ErrBadRoot":
  69. var e struct{ Error ErrBadRoot }
  70. err = json.Unmarshal(text, &e)
  71. theError = e.Error
  72. case "ErrBadTargets":
  73. var e struct{ Error ErrBadTargets }
  74. err = json.Unmarshal(text, &e)
  75. theError = e.Error
  76. case "ErrBadSnapshot":
  77. var e struct{ Error ErrBadSnapshot }
  78. err = json.Unmarshal(text, &e)
  79. theError = e.Error
  80. default:
  81. err = fmt.Errorf("do not know how to unmarshal %s", x.Name)
  82. return
  83. }
  84. if err != nil {
  85. return
  86. }
  87. s.Name = x.Name
  88. s.Error = theError
  89. return nil
  90. }
  91. // NewSerializableError serializes one of the above errors into JSON
  92. func NewSerializableError(err error) (*SerializableError, error) {
  93. // make sure it's one of our errors
  94. var name string
  95. switch err.(type) {
  96. case ErrValidation:
  97. name = "ErrValidation"
  98. case ErrBadHierarchy:
  99. name = "ErrBadHierarchy"
  100. case ErrBadRoot:
  101. name = "ErrBadRoot"
  102. case ErrBadTargets:
  103. name = "ErrBadTargets"
  104. case ErrBadSnapshot:
  105. name = "ErrBadSnapshot"
  106. default:
  107. return nil, fmt.Errorf("does not support serializing non-validation errors")
  108. }
  109. return &SerializableError{Name: name, Error: err}, nil
  110. }