errors.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package stun
  2. import "errors"
  3. // DecodeErr records an error and place when it is occurred.
  4. type DecodeErr struct {
  5. Place DecodeErrPlace
  6. Message string
  7. }
  8. // IsInvalidCookie returns true if error means that magic cookie
  9. // value is invalid.
  10. func (e DecodeErr) IsInvalidCookie() bool {
  11. return e.Place == DecodeErrPlace{"message", "cookie"}
  12. }
  13. // IsPlaceParent reports if error place parent is p.
  14. func (e DecodeErr) IsPlaceParent(p string) bool {
  15. return e.Place.Parent == p
  16. }
  17. // IsPlaceChildren reports if error place children is c.
  18. func (e DecodeErr) IsPlaceChildren(c string) bool {
  19. return e.Place.Children == c
  20. }
  21. // IsPlace reports if error place is p.
  22. func (e DecodeErr) IsPlace(p DecodeErrPlace) bool {
  23. return e.Place == p
  24. }
  25. // DecodeErrPlace records a place where error is occurred.
  26. type DecodeErrPlace struct {
  27. Parent string
  28. Children string
  29. }
  30. func (p DecodeErrPlace) String() string {
  31. return p.Parent + "/" + p.Children
  32. }
  33. func (e DecodeErr) Error() string {
  34. return "BadFormat for " + e.Place.String() + ": " + e.Message
  35. }
  36. func newDecodeErr(parent, children, message string) *DecodeErr {
  37. return &DecodeErr{
  38. Place: DecodeErrPlace{Parent: parent, Children: children},
  39. Message: message,
  40. }
  41. }
  42. // TODO(ar): rewrite errors to be more precise.
  43. func newAttrDecodeErr(children, message string) *DecodeErr {
  44. return newDecodeErr("attribute", children, message)
  45. }
  46. // ErrAttributeSizeInvalid means that decoded attribute size is invalid.
  47. var ErrAttributeSizeInvalid = errors.New("attribute size is invalid")
  48. // ErrAttributeSizeOverflow means that decoded attribute size is too big.
  49. var ErrAttributeSizeOverflow = errors.New("attribute size overflow")