OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package errors | 5 package errors |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 ) | 9 ) |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 return e | 45 return e |
46 } | 46 } |
47 } | 47 } |
48 return nil | 48 return nil |
49 } | 49 } |
50 | 50 |
51 func (m MultiError) stackContext() stackContext { | 51 func (m MultiError) stackContext() stackContext { |
52 n, _ := m.Summary() | 52 n, _ := m.Summary() |
53 | 53 |
54 return stackContext{ | 54 return stackContext{ |
55 » » internalReason: "MultiError %(non-nil)d/%(total)d: following fir
st non-nil error.", | 55 » » internalReason: fmt.Sprintf( |
56 » » data: Data{ | 56 » » » "MultiError %d/%d: following first non-nil error.", n, l
en(m)), |
57 » » » "non-nil": {Value: n}, | |
58 » » » "total": {Value: len(m)}, | |
59 » » }, | |
60 } | 57 } |
61 } | 58 } |
62 | 59 |
63 // NewMultiError create new multi error from given errors. | 60 // NewMultiError create new multi error from given errors. |
64 // | 61 // |
65 // Can be used to workaround 'go vet' confusion "composite literal uses unkeyed | 62 // Can be used to workaround 'go vet' confusion "composite literal uses unkeyed |
66 // fields" or if you do not want to remember that MultiError is in fact []error. | 63 // fields" or if you do not want to remember that MultiError is in fact []error. |
67 func NewMultiError(errors ...error) MultiError { | 64 func NewMultiError(errors ...error) MultiError { |
68 return errors | 65 return errors |
69 } | 66 } |
70 | 67 |
71 // SingleError provides a simple way to uwrap a MultiError if you know that it | 68 // SingleError provides a simple way to uwrap a MultiError if you know that it |
72 // could only ever contain one element. | 69 // could only ever contain one element. |
73 // | 70 // |
74 // If err is a MultiError, return its first element. Otherwise, return err. | 71 // If err is a MultiError, return its first element. Otherwise, return err. |
75 func SingleError(err error) error { | 72 func SingleError(err error) error { |
76 if me, ok := err.(MultiError); ok { | 73 if me, ok := err.(MultiError); ok { |
77 if len(me) == 0 { | 74 if len(me) == 0 { |
78 return nil | 75 return nil |
79 } | 76 } |
80 return me[0] | 77 return me[0] |
81 } | 78 } |
82 return err | 79 return err |
83 } | 80 } |
OLD | NEW |