error.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package amntfs
  2. import "fmt"
  3. type AMNTFSErrNo uint8
  4. const (
  5. ErrChildFsError AMNTFSErrNo = iota
  6. ErrUnknownInternalError AMNTFSErrNo = iota
  7. ErrMountPointAlreadyMounted AMNTFSErrNo = iota
  8. ErrNoAvailableMountPointForThisPath AMNTFSErrNo = iota
  9. )
  10. func (e AMNTFSErrNo) Message() string {
  11. switch e {
  12. case ErrMountPointAlreadyMounted:
  13. return "mount point already mounted"
  14. case ErrUnknownInternalError:
  15. return "unknown internal error"
  16. case ErrNoAvailableMountPointForThisPath:
  17. return "no available mount point for this path"
  18. case ErrChildFsError:
  19. return "child filesystem error"
  20. default:
  21. return "unknown error"
  22. }
  23. }
  24. var _ error = (*AMNTFSError)(nil)
  25. type AMNTFSError struct {
  26. etype AMNTFSErrNo
  27. fstype string
  28. mountid int64
  29. mountPoint string
  30. rawErr error
  31. }
  32. func (e AMNTFSError) Error() string {
  33. if e.etype == ErrChildFsError {
  34. return fmt.Sprintf("%s [mid=0x%08X]: %s", e.fstype, e.mountid, e.rawErr.Error())
  35. }
  36. if e.rawErr == nil {
  37. return fmt.Sprintf("amntfs: %s", e.etype.Message())
  38. } else {
  39. return fmt.Sprintf("amntfs: %s: %s", e.etype.Message(), e.rawErr.Error())
  40. }
  41. }
  42. func (e AMNTFSError) GetRawError() error {
  43. return e.rawErr
  44. }
  45. func (e AMNTFSError) GetErrNo() AMNTFSErrNo {
  46. return e.etype
  47. }
  48. func (e AMNTFSError) GetFsType() string {
  49. return e.fstype
  50. }
  51. func (e AMNTFSError) GetMountId() int64 {
  52. return e.mountid
  53. }
  54. func (e AMNTFSError) GetMountPoint() string {
  55. return e.mountPoint
  56. }
  57. func (e AMNTFSError) GetFsDescription() string {
  58. return fmt.Sprintf("%s (mid=0x%08X, mnt='%s')", e.fstype, e.mountid, e.mountPoint)
  59. }
  60. func NewAMNTFSError(emsg AMNTFSErrNo, rawErr error) *AMNTFSError {
  61. return &AMNTFSError{
  62. etype: emsg,
  63. rawErr: rawErr,
  64. fstype: "",
  65. mountPoint: "",
  66. mountid: 0,
  67. }
  68. }
  69. func NewAMNTFSChildFsError(fsent *MountNode, rawErr error) *AMNTFSError {
  70. return &AMNTFSError{
  71. etype: ErrChildFsError,
  72. rawErr: rawErr,
  73. fstype: fsent.FsType(),
  74. mountid: fsent.MountId(),
  75. mountPoint: fsent.mountPoint,
  76. }
  77. }
  78. func CheckErrorType(err error, errNo AMNTFSErrNo) bool {
  79. if err == nil {
  80. return false
  81. }
  82. switch err.(type) {
  83. case AMNTFSError:
  84. return err.(AMNTFSError).GetErrNo() == errNo
  85. default:
  86. return false
  87. }
  88. }