github.com/git-lfs/git-lfs@v2.5.2+incompatible/errors/context.go (about)

     1  package errors
     2  
     3  type withContext interface {
     4  	Set(string, interface{})
     5  	Get(string) interface{}
     6  	Del(string)
     7  	Context() map[string]interface{}
     8  }
     9  
    10  // ErrorSetContext sets a value in the error's context. If the error has not
    11  // been wrapped, it does nothing.
    12  func SetContext(err error, key string, value interface{}) {
    13  	if e, ok := err.(withContext); ok {
    14  		e.Set(key, value)
    15  	}
    16  }
    17  
    18  // ErrorGetContext gets a value from the error's context. If the error has not
    19  // been wrapped, it returns an empty string.
    20  func GetContext(err error, key string) interface{} {
    21  	if e, ok := err.(withContext); ok {
    22  		return e.Get(key)
    23  	}
    24  	return ""
    25  }
    26  
    27  // ErrorDelContext removes a value from the error's context. If the error has
    28  // not been wrapped, it does nothing.
    29  func DelContext(err error, key string) {
    30  	if e, ok := err.(withContext); ok {
    31  		e.Del(key)
    32  	}
    33  }
    34  
    35  // ErrorContext returns the context map for an error if it is a wrappedError.
    36  // If it is not a wrappedError it will return an empty map.
    37  func Context(err error) map[string]interface{} {
    38  	if e, ok := err.(withContext); ok {
    39  		return e.Context()
    40  	}
    41  	return nil
    42  }