| 1 | package errors |
| 2 | |
| 3 | import ( |
| 4 | goerrors "errors" |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | "os/exec" |
| 8 | "strconv" |
| 9 | "syscall" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/git-lfs/git-lfs/v3/tr" |
| 13 | "github.com/pkg/errors" |
| 14 | ) |
| 15 | |
| 16 | // IsFatalError indicates that the error is fatal and the process should exit |
| 17 | // immediately after handling the error. |
| 18 | func IsFatalError(err error) bool { |
| 19 | if e, ok := err.(interface { |
| 20 | Fatal() bool |
| 21 | }); ok { |
| 22 | return e.Fatal() |
| 23 | } |
| 24 | if parent := parentOf(err); parent != nil { |
| 25 | return IsFatalError(parent) |
| 26 | } |
| 27 | return false |
| 28 | } |
| 29 | |
| 30 | // IsNotImplementedError indicates the client attempted to use a feature the |
| 31 | // server has not implemented (e.g. the batch endpoint). |
| 32 | func IsNotImplementedError(err error) bool { |
| 33 | if e, ok := err.(interface { |
| 34 | NotImplemented() bool |
| 35 | }); ok { |
| 36 | return e.NotImplemented() |
| 37 | } |
| 38 | if parent := parentOf(err); parent != nil { |
| 39 | return IsNotImplementedError(parent) |
| 40 | } |
| 41 | return false |
| 42 | } |
| 43 | |
| 44 | // IsAuthError indicates the client provided a request with invalid or no |
| 45 | // authentication credentials when credentials are required (e.g. HTTP 401). |
| 46 | func IsAuthError(err error) bool { |
| 47 | if e, ok := err.(interface { |
| 48 | AuthError() bool |
| 49 | }); ok { |
| 50 | return e.AuthError() |
| 51 | } |
| 52 | if parent := parentOf(err); parent != nil { |
| 53 | return IsAuthError(parent) |
| 54 | } |
| 55 | return false |
| 56 | } |
| 57 | |
| 58 | // IsSmudgeError indicates an error while smudging a files. |
| 59 | func IsSmudgeError(err error) bool { |
| 60 | if e, ok := err.(interface { |
| 61 | SmudgeError() bool |
| 62 | }); ok { |
| 63 | return e.SmudgeError() |
| 64 | } |
| 65 | if parent := parentOf(err); parent != nil { |
| 66 | return IsSmudgeError(parent) |
| 67 | } |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | // IsCleanPointerError indicates an error while cleaning a file. |
| 72 | func IsCleanPointerError(err error) bool { |
| 73 | if e, ok := err.(interface { |
| 74 | CleanPointerError() bool |
| 75 | }); ok { |
| 76 | return e.CleanPointerError() |
| 77 | } |
| 78 | if parent := parentOf(err); parent != nil { |
| 79 | return IsCleanPointerError(parent) |
| 80 | } |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | // IsNotAPointerError indicates the parsed data is not an LFS pointer. |
| 85 | func IsNotAPointerError(err error) bool { |
| 86 | if e, ok := err.(interface { |
| 87 | NotAPointerError() bool |
| 88 | }); ok { |
| 89 | return e.NotAPointerError() |
| 90 | } |
| 91 | if parent := parentOf(err); parent != nil { |
| 92 | return IsNotAPointerError(parent) |
| 93 | } |
| 94 | return false |
| 95 | } |
| 96 | |
| 97 | // IsNotAPointerError indicates the parsed data is not an LFS pointer. |
| 98 | func IsPointerScanError(err error) bool { |
| 99 | if e, ok := err.(interface { |
| 100 | PointerScanError() bool |
| 101 | }); ok { |
| 102 | return e.PointerScanError() |
| 103 | } |
| 104 | if parent := parentOf(err); parent != nil { |
| 105 | return IsPointerScanError(parent) |
| 106 | } |
| 107 | return false |
| 108 | } |
| 109 | |
| 110 | // IsBadPointerKeyError indicates that the parsed data has an invalid key. |
| 111 | func IsBadPointerKeyError(err error) bool { |
| 112 | if e, ok := err.(interface { |
| 113 | BadPointerKeyError() bool |
| 114 | }); ok { |
| 115 | return e.BadPointerKeyError() |
| 116 | } |
| 117 | |
| 118 | if parent := parentOf(err); parent != nil { |
| 119 | return IsBadPointerKeyError(parent) |
| 120 | } |
| 121 | return false |
| 122 | } |
| 123 | |
| 124 | // IsProtocolError indicates that the SSH pkt-line protocol data is invalid. |
| 125 | func IsProtocolError(err error) bool { |
| 126 | if e, ok := err.(interface { |
| 127 | ProtocolError() bool |
| 128 | }); ok { |
| 129 | return e.ProtocolError() |
| 130 | } |
| 131 | |
| 132 | if parent := parentOf(err); parent != nil { |
| 133 | return IsProtocolError(parent) |
| 134 | } |
| 135 | return false |
| 136 | } |
| 137 | |
| 138 | // If an error is abad pointer error of any type, returns NotAPointerError |
| 139 | func StandardizeBadPointerError(err error) error { |
| 140 | if IsBadPointerKeyError(err) { |
| 141 | badErr := err.(badPointerKeyError) |
| 142 | if badErr.Expected == "version" { |
| 143 | return NewNotAPointerError(err) |
| 144 | } |
| 145 | } |
| 146 | return err |
| 147 | } |
| 148 | |
| 149 | // IsDownloadDeclinedError indicates that the smudge operation should not download. |
| 150 | // TODO: I don't really like using errors to control that flow, it should be refactored. |
| 151 | func IsDownloadDeclinedError(err error) bool { |
| 152 | if e, ok := err.(interface { |
| 153 | DownloadDeclinedError() bool |
| 154 | }); ok { |
| 155 | return e.DownloadDeclinedError() |
| 156 | } |
| 157 | if parent := parentOf(err); parent != nil { |
| 158 | return IsDownloadDeclinedError(parent) |
| 159 | } |
| 160 | return false |
| 161 | } |
| 162 | |
| 163 | // IsUnprocessableEntityError indicates that the upload operation failed because of |
| 164 | // an HTTP 422 response code. |
| 165 | func IsUnprocessableEntityError(err error) bool { |
| 166 | if e, ok := err.(interface { |
| 167 | UnprocessableEntityError() bool |
| 168 | }); ok { |
| 169 | return e.UnprocessableEntityError() |
| 170 | } |
| 171 | if parent := parentOf(err); parent != nil { |
| 172 | return IsUnprocessableEntityError(parent) |
| 173 | } |
| 174 | return false |
| 175 | } |
| 176 | |
| 177 | // IsRetriableError indicates the low level transfer had an error but the |
| 178 | // caller may retry the operation. |
| 179 | func IsRetriableError(err error) bool { |
| 180 | if e, ok := err.(interface { |
| 181 | RetriableError() bool |
| 182 | }); ok { |
| 183 | return e.RetriableError() |
| 184 | } |
| 185 | if cause, ok := Cause(err).(*url.Error); ok { |
| 186 | return cause.Temporary() || cause.Timeout() |
| 187 | } |
| 188 | if parent := parentOf(err); parent != nil { |
| 189 | return IsRetriableError(parent) |
| 190 | } |
| 191 | return false |
| 192 | } |
| 193 | |
| 194 | func IsRetriableLaterError(err error) (time.Time, bool) { |
| 195 | if e, ok := err.(interface { |
| 196 | RetriableLaterError() (time.Time, bool) |
| 197 | }); ok { |
| 198 | return e.RetriableLaterError() |
| 199 | } |
| 200 | if parent := parentOf(err); parent != nil { |
| 201 | return IsRetriableLaterError(parent) |
| 202 | } |
| 203 | return time.Time{}, false |
| 204 | } |
| 205 | |
| 206 | type errorWithCause interface { |
| 207 | Cause() error |
| 208 | error |
| 209 | fmt.Formatter |
| 210 | } |
| 211 | |
| 212 | // wrappedError is the base error wrapper. It provides a Message string, a |
| 213 | // stack, and a context map around a regular Go error. |
| 214 | type wrappedError struct { |
| 215 | errorWithCause |
| 216 | context map[string]interface{} |
| 217 | } |
| 218 | |
| 219 | // newWrappedError creates a wrappedError. |
| 220 | func newWrappedError(err error, message string) *wrappedError { |
| 221 | if err == nil { |
| 222 | err = errors.New(tr.Tr.Get("Error")) |
| 223 | } |
| 224 | |
| 225 | var errWithCause errorWithCause |
| 226 | |
| 227 | if len(message) > 0 { |
| 228 | errWithCause = errors.Wrap(err, message).(errorWithCause) |
| 229 | } else if ewc, ok := err.(errorWithCause); ok { |
| 230 | errWithCause = ewc |
| 231 | } else { |
| 232 | errWithCause = errors.Wrap(err, "LFS").(errorWithCause) |
| 233 | } |
| 234 | |
| 235 | return &wrappedError{ |
| 236 | context: make(map[string]interface{}), |
| 237 | errorWithCause: errWithCause, |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Set sets the value for the key in the context. |
| 242 | func (e wrappedError) Set(key string, val interface{}) { |
| 243 | e.context[key] = val |
| 244 | } |
| 245 | |
| 246 | // Get gets the value for a key in the context. |
| 247 | func (e wrappedError) Get(key string) interface{} { |
| 248 | return e.context[key] |
| 249 | } |
| 250 | |
| 251 | // Del removes a key from the context. |
| 252 | func (e wrappedError) Del(key string) { |
| 253 | delete(e.context, key) |
| 254 | } |
| 255 | |
| 256 | // Context returns the underlying context. |
| 257 | func (e wrappedError) Context() map[string]interface{} { |
| 258 | return e.context |
| 259 | } |
| 260 | |
| 261 | // Definitions for IsFatalError() |
| 262 | |
| 263 | type fatalError struct { |
| 264 | *wrappedError |
| 265 | } |
| 266 | |
| 267 | func (e fatalError) Fatal() bool { |
| 268 | return true |
| 269 | } |
| 270 | |
| 271 | func NewFatalError(err error) error { |
| 272 | return fatalError{newWrappedError(err, tr.Tr.Get("Fatal error"))} |
| 273 | } |
| 274 | |
| 275 | // Definitions for IsNotImplementedError() |
| 276 | |
| 277 | type notImplementedError struct { |
| 278 | *wrappedError |
| 279 | } |
| 280 | |
| 281 | func (e notImplementedError) NotImplemented() bool { |
| 282 | return true |
| 283 | } |
| 284 | |
| 285 | func NewNotImplementedError(err error) error { |
| 286 | return notImplementedError{newWrappedError(err, tr.Tr.Get("Not implemented"))} |
| 287 | } |
| 288 | |
| 289 | // Definitions for IsAuthError() |
| 290 | |
| 291 | type authError struct { |
| 292 | *wrappedError |
| 293 | } |
| 294 | |
| 295 | func (e authError) AuthError() bool { |
| 296 | return true |
| 297 | } |
| 298 | |
| 299 | func NewAuthError(err error) error { |
| 300 | return authError{newWrappedError(err, tr.Tr.Get("Authentication required"))} |
| 301 | } |
| 302 | |
| 303 | // Definitions for IsSmudgeError() |
| 304 | |
| 305 | type smudgeError struct { |
| 306 | *wrappedError |
| 307 | } |
| 308 | |
| 309 | func (e smudgeError) SmudgeError() bool { |
| 310 | return true |
| 311 | } |
| 312 | |
| 313 | func NewSmudgeError(err error, oid, filename string) error { |
| 314 | e := smudgeError{newWrappedError(err, tr.Tr.Get("Smudge error"))} |
| 315 | SetContext(e, "OID", oid) |
| 316 | SetContext(e, "FileName", filename) |
| 317 | return e |
| 318 | } |
| 319 | |
| 320 | // Definitions for IsCleanPointerError() |
| 321 | |
| 322 | type cleanPointerError struct { |
| 323 | *wrappedError |
| 324 | } |
| 325 | |
| 326 | func (e cleanPointerError) CleanPointerError() bool { |
| 327 | return true |
| 328 | } |
| 329 | |
| 330 | func NewCleanPointerError(pointer interface{}, bytes []byte) error { |
| 331 | err := New(tr.Tr.Get("pointer error")) |
| 332 | e := cleanPointerError{newWrappedError(err, "clean")} |
| 333 | SetContext(e, "pointer", pointer) |
| 334 | SetContext(e, "bytes", bytes) |
| 335 | return e |
| 336 | } |
| 337 | |
| 338 | // Definitions for IsNotAPointerError() |
| 339 | |
| 340 | type notAPointerError struct { |
| 341 | *wrappedError |
| 342 | } |
| 343 | |
| 344 | func (e notAPointerError) NotAPointerError() bool { |
| 345 | return true |
| 346 | } |
| 347 | |
| 348 | func NewNotAPointerError(err error) error { |
| 349 | return notAPointerError{newWrappedError(err, tr.Tr.Get("Pointer file error"))} |
| 350 | } |
| 351 | |
| 352 | // Definitions for IsPointerScanError() |
| 353 | |
| 354 | type PointerScanError struct { |
| 355 | treeishOid string |
| 356 | path string |
| 357 | *wrappedError |
| 358 | } |
| 359 | |
| 360 | func (e PointerScanError) PointerScanError() bool { |
| 361 | return true |
| 362 | } |
| 363 | |
| 364 | func (e PointerScanError) OID() string { |
| 365 | return e.treeishOid |
| 366 | } |
| 367 | |
| 368 | func (e PointerScanError) Path() string { |
| 369 | return e.path |
| 370 | } |
| 371 | |
| 372 | func NewPointerScanError(err error, treeishOid, path string) error { |
| 373 | return PointerScanError{treeishOid, path, newWrappedError(err, tr.Tr.Get("Pointer error"))} |
| 374 | } |
| 375 | |
| 376 | type badPointerKeyError struct { |
| 377 | Expected string |
| 378 | Actual string |
| 379 | |
| 380 | *wrappedError |
| 381 | } |
| 382 | |
| 383 | func (e badPointerKeyError) BadPointerKeyError() bool { |
| 384 | return true |
| 385 | } |
| 386 | |
| 387 | func NewBadPointerKeyError(expected, actual string) error { |
| 388 | err := New(tr.Tr.Get("Expected key %s, got %s", expected, actual)) |
| 389 | return badPointerKeyError{expected, actual, newWrappedError(err, tr.Tr.Get("pointer parsing"))} |
| 390 | } |
| 391 | |
| 392 | // Definitions for IsDownloadDeclinedError() |
| 393 | |
| 394 | type downloadDeclinedError struct { |
| 395 | *wrappedError |
| 396 | } |
| 397 | |
| 398 | func (e downloadDeclinedError) DownloadDeclinedError() bool { |
| 399 | return true |
| 400 | } |
| 401 | |
| 402 | func NewDownloadDeclinedError(err error, msg string) error { |
| 403 | return downloadDeclinedError{newWrappedError(err, msg)} |
| 404 | } |
| 405 | |
| 406 | // Definitions for IsRetriableLaterError() |
| 407 | |
| 408 | type retriableLaterError struct { |
| 409 | *wrappedError |
| 410 | timeAvailable time.Time |
| 411 | } |
| 412 | |
| 413 | func NewRetriableLaterError(err error, header string) error { |
| 414 | if header == "" { |
| 415 | return nil |
| 416 | } |
| 417 | |
| 418 | secs, parseErr := strconv.Atoi(header) |
| 419 | if parseErr == nil { |
| 420 | return retriableLaterError{ |
| 421 | wrappedError: newWrappedError(err, ""), |
| 422 | timeAvailable: time.Now().Add(time.Duration(secs) * time.Second), |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | parseTime, parseErr := time.Parse(time.RFC1123, header) |
| 427 | if parseErr == nil { |
| 428 | return retriableLaterError{ |
| 429 | wrappedError: newWrappedError(err, ""), |
| 430 | timeAvailable: parseTime, |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // We could not return a successful error from the Retry-After header. |
| 435 | return nil |
| 436 | } |
| 437 | |
| 438 | func (e retriableLaterError) RetriableLaterError() (time.Time, bool) { |
| 439 | return e.timeAvailable, true |
| 440 | } |
| 441 | |
| 442 | // Definitions for IsUnprocessableEntityError() |
| 443 | |
| 444 | type unprocessableEntityError struct { |
| 445 | *wrappedError |
| 446 | } |
| 447 | |
| 448 | func (e unprocessableEntityError) UnprocessableEntityError() bool { |
| 449 | return true |
| 450 | } |
| 451 | |
| 452 | func NewUnprocessableEntityError(err error) error { |
| 453 | return unprocessableEntityError{newWrappedError(err, "")} |
| 454 | } |
| 455 | |
| 456 | // Definitions for IsRetriableError() |
| 457 | |
| 458 | type retriableError struct { |
| 459 | *wrappedError |
| 460 | } |
| 461 | |
| 462 | func (e retriableError) RetriableError() bool { |
| 463 | return true |
| 464 | } |
| 465 | |
| 466 | func NewRetriableError(err error) error { |
| 467 | return retriableError{newWrappedError(err, "")} |
| 468 | } |
| 469 | |
| 470 | // Definitions for IsProtocolError() |
| 471 | |
| 472 | type protocolError struct { |
| 473 | *wrappedError |
| 474 | } |
| 475 | |
| 476 | func (e protocolError) ProtocolError() bool { |
| 477 | return true |
| 478 | } |
| 479 | |
| 480 | func NewProtocolError(message string, err error) error { |
| 481 | return protocolError{newWrappedError(err, message)} |
| 482 | } |
| 483 | |
| 484 | func parentOf(err error) error { |
| 485 | type causer interface { |
| 486 | Cause() error |
| 487 | } |
| 488 | |
| 489 | if c, ok := err.(causer); ok { |
| 490 | if innerC, innerOk := c.Cause().(causer); innerOk { |
| 491 | return innerC.Cause() |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return nil |
| 496 | } |
| 497 | |
| 498 | func ExitStatus(err error) int { |
| 499 | var eerr *exec.ExitError |
| 500 | if goerrors.As(err, &eerr) { |
| 501 | ws, ok := eerr.ProcessState.Sys().(syscall.WaitStatus) |
| 502 | if ok { |
| 503 | return ws.ExitStatus() |
| 504 | } |
| 505 | } |
| 506 | return -1 |
| 507 | } |