WithData returns a new error that wraps err as a chain error message containing a value of type map[string]interface{} as an extra data item. The map contains the values in the map in err, if any, plus the items in keyval. Keyval takes the form k1, v1, k2, v2, ... Values kN must be strings. Calling
(err error, keyval ...interface{})
| 144 | // Note that if err already has a data item of any other type, |
| 145 | // it will not be accessible via the returned error value. |
| 146 | func WithData(err error, keyval ...interface{}) error { |
| 147 | if err == nil { |
| 148 | return nil |
| 149 | } |
| 150 | // TODO(kr): add vet check for odd-length keyval and non-string keys |
| 151 | newkv := make(map[string]interface{}) |
| 152 | for k, v := range Data(err) { |
| 153 | newkv[k] = v |
| 154 | } |
| 155 | for i := 0; i < len(keyval); i += 2 { |
| 156 | newkv[keyval[i].(string)] = keyval[i+1] |
| 157 | } |
| 158 | return withData(err, newkv) |
| 159 | } |
| 160 | |
| 161 | // Data returns the data item in err, if any. |
| 162 | func Data(err error) map[string]interface{} { |