error.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. rawErr error
  30. }
  31. func (e AMNTFSError) Error() string {
  32. if e.etype == ErrChildFsError {
  33. return fmt.Sprintf("%s [mid=0x%08X]: %s", e.fstype, e.mountid, e.rawErr.Error())
  34. }
  35. if e.rawErr == nil {
  36. return fmt.Sprintf("amntfs: %s", e.etype.Message())
  37. } else {
  38. return fmt.Sprintf("amntfs: %s: %s", e.etype.Message(), e.rawErr.Error())
  39. }
  40. }
  41. func (e AMNTFSError) GetRawError() error {
  42. return e.rawErr
  43. }
  44. func (e AMNTFSError) GetErrNo() AMNTFSErrNo {
  45. return e.etype
  46. }
  47. func (e AMNTFSError) GetFsType() string {
  48. return e.fstype
  49. }
  50. func (e AMNTFSError) GetMountId() int64 {
  51. return e.mountid
  52. }
  53. func (e AMNTFSError) GetFsDescription() string {
  54. return fmt.Sprintf("%s (mid=0x%08X)", e.fstype, e.mountid)
  55. }
  56. func NewAMNTFSError(emsg AMNTFSErrNo, rawErr error) *AMNTFSError {
  57. return &AMNTFSError{
  58. etype: emsg,
  59. rawErr: rawErr,
  60. fstype: "",
  61. mountid: 0,
  62. }
  63. }
  64. func NewAMNTFSChildFsError(fsent *MountNode, rawErr error) *AMNTFSError {
  65. return &AMNTFSError{
  66. etype: ErrChildFsError,
  67. rawErr: rawErr,
  68. fstype: fsent.FsType(),
  69. mountid: fsent.MountId(),
  70. }
  71. }
  72. func CheckErrorType(err error, errNo AMNTFSErrNo) bool {
  73. if err == nil {
  74. return false
  75. }
  76. switch err.(type) {
  77. case AMNTFSError:
  78. return err.(AMNTFSError).GetErrNo() == errNo
  79. default:
  80. return false
  81. }
  82. }