git-lfs/errors/context.go

43 lines
1.1 KiB
Go
Raw Normal View History

2016-08-18 22:15:15 +00:00
package errors
2016-08-19 20:03:39 +00:00
type withContext interface {
2016-08-18 22:15:15 +00:00
Set(string, interface{})
Get(string) interface{}
Del(string)
Context() map[string]interface{}
}
// ErrorSetContext sets a value in the error's context. If the error has not
// been wrapped, it does nothing.
2016-08-19 20:03:39 +00:00
func SetContext(err error, key string, value interface{}) {
if e, ok := err.(withContext); ok {
2016-08-18 22:15:15 +00:00
e.Set(key, value)
}
}
// ErrorGetContext gets a value from the error's context. If the error has not
// been wrapped, it returns an empty string.
2016-08-19 20:03:39 +00:00
func GetContext(err error, key string) interface{} {
if e, ok := err.(withContext); ok {
2016-08-18 22:15:15 +00:00
return e.Get(key)
}
return ""
}
// ErrorDelContext removes a value from the error's context. If the error has
// not been wrapped, it does nothing.
2016-08-19 20:03:39 +00:00
func DelContext(err error, key string) {
if e, ok := err.(withContext); ok {
2016-08-18 22:15:15 +00:00
e.Del(key)
}
}
// ErrorContext returns the context map for an error if it is a wrappedError.
// If it is not a wrappedError it will return an empty map.
2016-08-19 20:03:39 +00:00
func Context(err error) map[string]interface{} {
if e, ok := err.(withContext); ok {
2016-08-18 22:15:15 +00:00
return e.Context()
}
return nil
}