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