errors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package protocol
  4. import (
  5. "errors"
  6. "fmt"
  7. "net"
  8. )
  9. var (
  10. errBufferTooSmall = &TemporaryError{Err: errors.New("buffer is too small")} //nolint:goerr113
  11. errInvalidCipherSpec = &FatalError{Err: errors.New("cipher spec invalid")} //nolint:goerr113
  12. )
  13. // FatalError indicates that the DTLS connection is no longer available.
  14. // It is mainly caused by wrong configuration of server or client.
  15. type FatalError struct {
  16. Err error
  17. }
  18. // InternalError indicates and internal error caused by the implementation, and the DTLS connection is no longer available.
  19. // It is mainly caused by bugs or tried to use unimplemented features.
  20. type InternalError struct {
  21. Err error
  22. }
  23. // TemporaryError indicates that the DTLS connection is still available, but the request was failed temporary.
  24. type TemporaryError struct {
  25. Err error
  26. }
  27. // TimeoutError indicates that the request was timed out.
  28. type TimeoutError struct {
  29. Err error
  30. }
  31. // HandshakeError indicates that the handshake failed.
  32. type HandshakeError struct {
  33. Err error
  34. }
  35. // Timeout implements net.Error.Timeout()
  36. func (*FatalError) Timeout() bool { return false }
  37. // Temporary implements net.Error.Temporary()
  38. func (*FatalError) Temporary() bool { return false }
  39. // Unwrap implements Go1.13 error unwrapper.
  40. func (e *FatalError) Unwrap() error { return e.Err }
  41. func (e *FatalError) Error() string { return fmt.Sprintf("dtls fatal: %v", e.Err) }
  42. // Timeout implements net.Error.Timeout()
  43. func (*InternalError) Timeout() bool { return false }
  44. // Temporary implements net.Error.Temporary()
  45. func (*InternalError) Temporary() bool { return false }
  46. // Unwrap implements Go1.13 error unwrapper.
  47. func (e *InternalError) Unwrap() error { return e.Err }
  48. func (e *InternalError) Error() string { return fmt.Sprintf("dtls internal: %v", e.Err) }
  49. // Timeout implements net.Error.Timeout()
  50. func (*TemporaryError) Timeout() bool { return false }
  51. // Temporary implements net.Error.Temporary()
  52. func (*TemporaryError) Temporary() bool { return true }
  53. // Unwrap implements Go1.13 error unwrapper.
  54. func (e *TemporaryError) Unwrap() error { return e.Err }
  55. func (e *TemporaryError) Error() string { return fmt.Sprintf("dtls temporary: %v", e.Err) }
  56. // Timeout implements net.Error.Timeout()
  57. func (*TimeoutError) Timeout() bool { return true }
  58. // Temporary implements net.Error.Temporary()
  59. func (*TimeoutError) Temporary() bool { return true }
  60. // Unwrap implements Go1.13 error unwrapper.
  61. func (e *TimeoutError) Unwrap() error { return e.Err }
  62. func (e *TimeoutError) Error() string { return fmt.Sprintf("dtls timeout: %v", e.Err) }
  63. // Timeout implements net.Error.Timeout()
  64. func (e *HandshakeError) Timeout() bool {
  65. var netErr net.Error
  66. if errors.As(e.Err, &netErr) {
  67. return netErr.Timeout()
  68. }
  69. return false
  70. }
  71. // Temporary implements net.Error.Temporary()
  72. func (e *HandshakeError) Temporary() bool {
  73. var netErr net.Error
  74. if errors.As(e.Err, &netErr) {
  75. return netErr.Temporary() //nolint
  76. }
  77. return false
  78. }
  79. // Unwrap implements Go1.13 error unwrapper.
  80. func (e *HandshakeError) Unwrap() error { return e.Err }
  81. func (e *HandshakeError) Error() string { return fmt.Sprintf("handshake error: %v", e.Err) }