github.com/mithrandie/csvq@v1.18.1/lib/file/error.go (about) 1 package file 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/mithrandie/go-file/v2" 8 ) 9 10 func ParseError(err error) error { 11 switch err.(type) { 12 case *file.IOError: 13 return NewIOError(err.Error()) 14 case *file.LockError: 15 return NewLockError(err.Error()) 16 case *file.TimeoutError: 17 return &TimeoutError{ 18 message: err.Error(), 19 } 20 case *file.ContextCanceled: 21 return NewContextCanceled() 22 case *file.ContextDone: 23 return NewContextDone(err.Error()) 24 default: 25 return err 26 } 27 } 28 29 type IOError struct { 30 message string 31 } 32 33 func NewIOError(message string) error { 34 return &IOError{ 35 message: message, 36 } 37 } 38 39 func (e IOError) Error() string { 40 return e.message 41 } 42 43 type NotExistError struct { 44 message string 45 } 46 47 func NewNotExistError(message string) error { 48 return &NotExistError{ 49 message: message, 50 } 51 } 52 53 func (e NotExistError) Error() string { 54 return e.message 55 } 56 57 type AlreadyExistError struct { 58 message string 59 } 60 61 func NewAlreadyExistError(message string) error { 62 return &AlreadyExistError{ 63 message: message, 64 } 65 } 66 67 func (e AlreadyExistError) Error() string { 68 return e.message 69 } 70 71 type LockError struct { 72 message string 73 } 74 75 func NewLockError(message string) error { 76 return &LockError{ 77 message: message, 78 } 79 } 80 81 func (e LockError) Error() string { 82 return e.message 83 } 84 85 type TimeoutError struct { 86 message string 87 } 88 89 func NewTimeoutError(path string) error { 90 return &TimeoutError{ 91 message: fmt.Sprintf("file %s: lock waiting time exceeded", path), 92 } 93 } 94 95 func (e TimeoutError) Error() string { 96 return e.message 97 } 98 99 type ContextCanceled struct { 100 message string 101 } 102 103 func NewContextCanceled() error { 104 return &ContextCanceled{ 105 message: "execution canceled", 106 } 107 } 108 109 func (e ContextCanceled) Error() string { 110 return e.message 111 } 112 113 type ContextDone struct { 114 message string 115 } 116 117 func NewContextDone(message string) error { 118 return &ContextDone{ 119 message: message, 120 } 121 } 122 123 func (e ContextDone) Error() string { 124 return e.message 125 } 126 127 type ForcedUnlockError struct { 128 Errors []error 129 } 130 131 func NewForcedUnlockError(errs []error) error { 132 if errs == nil { 133 return nil 134 } 135 136 return &ForcedUnlockError{ 137 Errors: errs, 138 } 139 } 140 141 func (e ForcedUnlockError) Error() string { 142 list := make([]string, 0, len(e.Errors)) 143 for _, err := range e.Errors { 144 list = append(list, err.Error()) 145 } 146 return strings.Join(list, "\n ") 147 } 148 149 type CompositeError struct { 150 message string 151 } 152 153 func NewCompositeError(err1 error, err2 error) error { 154 if err1 == nil { 155 return err2 156 } 157 if err2 == nil { 158 return err1 159 } 160 161 return &CompositeError{ 162 message: err1.Error() + "\n " + err2.Error(), 163 } 164 } 165 166 func (e CompositeError) Error() string { 167 return e.message 168 }