errors.go 1.7 KB

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