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 30 matching lines...) Expand all Loading... |
41 // First returns the first non-nil error. | 41 // First returns the first non-nil error. |
42 func (m MultiError) First() error { | 42 func (m MultiError) First() error { |
43 for _, e := range m { | 43 for _, e := range m { |
44 if e != nil { | 44 if e != nil { |
45 return e | 45 return e |
46 } | 46 } |
47 } | 47 } |
48 return nil | 48 return nil |
49 } | 49 } |
50 | 50 |
51 // StackContext implements StackContexter. | 51 func (m MultiError) stackContext() stackContext { |
52 func (m MultiError) StackContext() StackContext { | |
53 n, _ := m.Summary() | 52 n, _ := m.Summary() |
54 | 53 |
55 » return StackContext{ | 54 » return stackContext{ |
56 » » InternalReason: "MultiError %(non-nil)d/%(total)d: following fir
st non-nil error.", | 55 » » internalReason: "MultiError %(non-nil)d/%(total)d: following fir
st non-nil error.", |
57 » » Data: Data{ | 56 » » data: Data{ |
58 "non-nil": {Value: n}, | 57 "non-nil": {Value: n}, |
59 "total": {Value: len(m)}, | 58 "total": {Value: len(m)}, |
60 }, | 59 }, |
61 } | 60 } |
62 } | 61 } |
63 | 62 |
64 // NewMultiError create new multi error from given errors. | 63 // NewMultiError create new multi error from given errors. |
65 // | 64 // |
66 // Can be used to workaround 'go vet' confusion "composite literal uses unkeyed | 65 // Can be used to workaround 'go vet' confusion "composite literal uses unkeyed |
67 // fields" or if you do not want to remember that MultiError is in fact []error. | 66 // fields" or if you do not want to remember that MultiError is in fact []error. |
68 func NewMultiError(errors ...error) MultiError { | 67 func NewMultiError(errors ...error) MultiError { |
69 return errors | 68 return errors |
70 } | 69 } |
71 | 70 |
72 // SingleError provides a simple way to uwrap a MultiError if you know that it | 71 // SingleError provides a simple way to uwrap a MultiError if you know that it |
73 // could only ever contain one element. | 72 // could only ever contain one element. |
74 // | 73 // |
75 // If err is a MultiError, return its first element. Otherwise, return err. | 74 // If err is a MultiError, return its first element. Otherwise, return err. |
76 func SingleError(err error) error { | 75 func SingleError(err error) error { |
77 if me, ok := err.(MultiError); ok { | 76 if me, ok := err.(MultiError); ok { |
78 if len(me) == 0 { | 77 if len(me) == 0 { |
79 return nil | 78 return nil |
80 } | 79 } |
81 return me[0] | 80 return me[0] |
82 } | 81 } |
83 return err | 82 return err |
84 } | 83 } |
OLD | NEW |