errors.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package stun
  2. import "errors"
  3. // DecodeErr records an error and place when it is occurred.
  4. // nolint:errname
  5. type DecodeErr struct {
  6. Place DecodeErrPlace
  7. Message string
  8. }
  9. // IsInvalidCookie returns true if error means that magic cookie
  10. // value is invalid.
  11. func (e DecodeErr) IsInvalidCookie() bool {
  12. return e.Place == DecodeErrPlace{"message", "cookie"}
  13. }
  14. // IsPlaceParent reports if error place parent is p.
  15. func (e DecodeErr) IsPlaceParent(p string) bool {
  16. return e.Place.Parent == p
  17. }
  18. // IsPlaceChildren reports if error place children is c.
  19. func (e DecodeErr) IsPlaceChildren(c string) bool {
  20. return e.Place.Children == c
  21. }
  22. // IsPlace reports if error place is p.
  23. func (e DecodeErr) IsPlace(p DecodeErrPlace) bool {
  24. return e.Place == p
  25. }
  26. // DecodeErrPlace records a place where error is occurred.
  27. type DecodeErrPlace struct {
  28. Parent string
  29. Children string
  30. }
  31. func (p DecodeErrPlace) String() string {
  32. return p.Parent + "/" + p.Children
  33. }
  34. func (e DecodeErr) Error() string {
  35. return "BadFormat for " + e.Place.String() + ": " + e.Message
  36. }
  37. func newDecodeErr(parent, children, message string) *DecodeErr {
  38. return &DecodeErr{
  39. Place: DecodeErrPlace{Parent: parent, Children: children},
  40. Message: message,
  41. }
  42. }
  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")