Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
90bf34ca
Commit
90bf34ca
authored
Sep 02, 2016
by
m1093782566
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix errors.NewAggregate nil pointer panic
Change-Id: I82b376898f9170d237a3bd1a1ac898cc6c636d12
parent
c9fda51d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
1 deletion
+53
-1
errors.go
pkg/util/errors/errors.go
+13
-1
errors_test.go
pkg/util/errors/errors_test.go
+40
-0
No files found.
pkg/util/errors/errors.go
View file @
90bf34ca
...
...
@@ -31,11 +31,23 @@ type Aggregate interface {
// NewAggregate converts a slice of errors into an Aggregate interface, which
// is itself an implementation of the error interface. If the slice is empty,
// this returns nil.
// It will check if any of the element of input error list is nil, to avoid
// nil pointer panic when call Error().
func
NewAggregate
(
errlist
[]
error
)
Aggregate
{
if
len
(
errlist
)
==
0
{
return
nil
}
return
aggregate
(
errlist
)
// In case of input error list contains nil
var
errs
[]
error
for
_
,
e
:=
range
errlist
{
if
e
!=
nil
{
errs
=
append
(
errs
,
e
)
}
}
if
len
(
errs
)
==
0
{
return
nil
}
return
aggregate
(
errs
)
}
// This helper implements the error and Errors interfaces. Keeping it private
...
...
pkg/util/errors/errors_test.go
View file @
90bf34ca
...
...
@@ -50,6 +50,46 @@ func TestEmptyAggregate(t *testing.T) {
}
}
func
TestAggregateWithNil
(
t
*
testing
.
T
)
{
var
slice
[]
error
slice
=
[]
error
{
nil
}
var
agg
Aggregate
var
err
error
agg
=
NewAggregate
(
slice
)
if
agg
!=
nil
{
t
.
Errorf
(
"expected nil, got %#v"
,
agg
)
}
err
=
NewAggregate
(
slice
)
if
err
!=
nil
{
t
.
Errorf
(
"expected nil, got %#v"
,
err
)
}
// Append a non-nil error
slice
=
append
(
slice
,
fmt
.
Errorf
(
"err"
))
agg
=
NewAggregate
(
slice
)
if
agg
==
nil
{
t
.
Errorf
(
"expected non-nil"
)
}
if
s
:=
agg
.
Error
();
s
!=
"err"
{
t
.
Errorf
(
"expected 'err', got %q"
,
s
)
}
if
s
:=
agg
.
Errors
();
len
(
s
)
!=
1
{
t
.
Errorf
(
"expected one-element slice, got %#v"
,
s
)
}
if
s
:=
agg
.
Errors
()[
0
]
.
Error
();
s
!=
"err"
{
t
.
Errorf
(
"expected 'err', got %q"
,
s
)
}
err
=
agg
.
(
error
)
if
err
==
nil
{
t
.
Errorf
(
"expected non-nil"
)
}
if
s
:=
err
.
Error
();
s
!=
"err"
{
t
.
Errorf
(
"expected 'err', got %q"
,
s
)
}
}
func
TestSingularAggregate
(
t
*
testing
.
T
)
{
var
slice
[]
error
=
[]
error
{
fmt
.
Errorf
(
"err"
)}
var
agg
Aggregate
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment