errors.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package netlink
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "os"
  7. "strings"
  8. )
  9. // Error messages which can be returned by Validate.
  10. var (
  11. errMismatchedSequence = errors.New("mismatched sequence in netlink reply")
  12. errMismatchedPID = errors.New("mismatched PID in netlink reply")
  13. errShortErrorMessage = errors.New("not enough data for netlink error code")
  14. )
  15. // Errors which can be returned by a Socket that does not implement
  16. // all exposed methods of Conn.
  17. var errNotSupported = errors.New("operation not supported")
  18. // notSupported provides a concise constructor for "not supported" errors.
  19. func notSupported(op string) error {
  20. return newOpError(op, errNotSupported)
  21. }
  22. // IsNotExist determines if an error is produced as the result of querying some
  23. // file, object, resource, etc. which does not exist. Users of this package
  24. // should always use netlink.IsNotExist, rather than os.IsNotExist, when
  25. // checking for specific netlink-related errors.
  26. //
  27. // Errors types created by this package, such as OpError, can be used with
  28. // IsNotExist, but this function also defers to the behavior of os.IsNotExist
  29. // for unrecognized error types.
  30. //
  31. // Deprecated: make use of errors.Unwrap and errors.Is in Go 1.13+.
  32. func IsNotExist(err error) bool {
  33. switch err := err.(type) {
  34. case *OpError:
  35. // Unwrap the inner error and use the stdlib's logic.
  36. return os.IsNotExist(err.Err)
  37. default:
  38. return os.IsNotExist(err)
  39. }
  40. }
  41. var (
  42. _ error = &OpError{}
  43. _ net.Error = &OpError{}
  44. // Ensure compatibility with Go 1.13+ errors package.
  45. _ interface{ Unwrap() error } = &OpError{}
  46. )
  47. // An OpError is an error produced as the result of a failed netlink operation.
  48. type OpError struct {
  49. // Op is the operation which caused this OpError, such as "send"
  50. // or "receive".
  51. Op string
  52. // Err is the underlying error which caused this OpError.
  53. //
  54. // If Err was produced by a system call error, Err will be of type
  55. // *os.SyscallError. If Err was produced by an error code in a netlink
  56. // message, Err will contain a raw error value type such as a unix.Errno.
  57. //
  58. // Most callers should inspect Err using errors.Is from the standard
  59. // library.
  60. Err error
  61. // Message and Offset contain additional error information provided by the
  62. // kernel when the ExtendedAcknowledge option is set on a Conn and the
  63. // kernel indicates the AcknowledgeTLVs flag in a response. If this option
  64. // is not set, both of these fields will be empty.
  65. Message string
  66. Offset int
  67. }
  68. // newOpError is a small wrapper for creating an OpError. As a convenience, it
  69. // returns nil if the input err is nil: akin to os.NewSyscallError.
  70. func newOpError(op string, err error) error {
  71. if err == nil {
  72. return nil
  73. }
  74. return &OpError{
  75. Op: op,
  76. Err: err,
  77. }
  78. }
  79. func (e *OpError) Error() string {
  80. if e == nil {
  81. return "<nil>"
  82. }
  83. var sb strings.Builder
  84. _, _ = sb.WriteString(fmt.Sprintf("netlink %s: %v", e.Op, e.Err))
  85. if e.Message != "" || e.Offset != 0 {
  86. _, _ = sb.WriteString(fmt.Sprintf(", offset: %d, message: %q",
  87. e.Offset, e.Message))
  88. }
  89. return sb.String()
  90. }
  91. // Unwrap unwraps the internal Err field for use with errors.Unwrap.
  92. func (e *OpError) Unwrap() error { return e.Err }
  93. // Portions of this code taken from the Go standard library:
  94. //
  95. // Copyright 2009 The Go Authors. All rights reserved.
  96. // Use of this source code is governed by a BSD-style
  97. // license that can be found in the LICENSE file.
  98. type timeout interface {
  99. Timeout() bool
  100. }
  101. // Timeout reports whether the error was caused by an I/O timeout.
  102. func (e *OpError) Timeout() bool {
  103. if ne, ok := e.Err.(*os.SyscallError); ok {
  104. t, ok := ne.Err.(timeout)
  105. return ok && t.Timeout()
  106. }
  107. t, ok := e.Err.(timeout)
  108. return ok && t.Timeout()
  109. }
  110. type temporary interface {
  111. Temporary() bool
  112. }
  113. // Temporary reports whether an operation may succeed if retried.
  114. func (e *OpError) Temporary() bool {
  115. if ne, ok := e.Err.(*os.SyscallError); ok {
  116. t, ok := ne.Err.(temporary)
  117. return ok && t.Temporary()
  118. }
  119. t, ok := e.Err.(temporary)
  120. return ok && t.Temporary()
  121. }