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
b08e5b24
Commit
b08e5b24
authored
Nov 21, 2014
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change ValidationError to a pointer
In Prep for adding at leats one more field, make this operate more list StatusError.
parent
d4c37e35
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
59 additions
and
60 deletions
+59
-60
errors.go
pkg/api/errors/errors.go
+11
-11
errors_test.go
pkg/api/errors/errors_test.go
+1
-1
validation.go
pkg/api/errors/validation.go
+22
-20
validation_test.go
pkg/api/errors/validation_test.go
+10
-10
validation_test.go
pkg/api/validation/validation_test.go
+7
-7
config.go
pkg/config/config.go
+5
-8
config_test.go
pkg/config/config_test.go
+3
-3
No files found.
pkg/api/errors/errors.go
View file @
b08e5b24
...
...
@@ -30,6 +30,8 @@ type StatusError struct {
ErrStatus
api
.
Status
}
var
_
error
=
&
statusError
{}
// Error implements the Error interface.
func
(
e
*
StatusError
)
Error
()
string
{
return
e
.
ErrStatus
.
Message
...
...
@@ -107,7 +109,7 @@ func NewConflict(kind, name string, err error) error {
func
NewInvalid
(
kind
,
name
string
,
errs
ValidationErrorList
)
error
{
causes
:=
make
([]
api
.
StatusCause
,
0
,
len
(
errs
))
for
i
:=
range
errs
{
if
err
,
ok
:=
errs
[
i
]
.
(
ValidationError
);
ok
{
if
err
,
ok
:=
errs
[
i
]
.
(
*
ValidationError
);
ok
{
causes
=
append
(
causes
,
api
.
StatusCause
{
Type
:
api
.
CauseType
(
err
.
Type
),
Message
:
err
.
Error
(),
...
...
@@ -130,18 +132,16 @@ func NewInvalid(kind, name string, errs ValidationErrorList) error {
// NewBadRequest creates an error that indicates that the request is invalid and can not be processed.
func
NewBadRequest
(
reason
string
)
error
{
return
&
StatusError
{
api
.
Status
{
Status
:
api
.
StatusFailure
,
Code
:
http
.
StatusBadRequest
,
Reason
:
api
.
StatusReasonBadRequest
,
Details
:
&
api
.
StatusDetails
{
Causes
:
[]
api
.
StatusCause
{
{
Message
:
reason
},
},
return
&
StatusError
{
api
.
Status
{
Status
:
api
.
StatusFailure
,
Code
:
http
.
StatusBadRequest
,
Reason
:
api
.
StatusReasonBadRequest
,
Details
:
&
api
.
StatusDetails
{
Causes
:
[]
api
.
StatusCause
{
{
Message
:
reason
},
},
},
}
}
}
}
// NewInternalError returns an error indicating the item is invalid and cannot be processed.
...
...
pkg/api/errors/errors_test.go
View file @
b08e5b24
...
...
@@ -54,7 +54,7 @@ func TestErrorNew(t *testing.T) {
func
TestNewInvalid
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
Err
ValidationError
Err
*
ValidationError
Details
*
api
.
StatusDetails
}{
{
...
...
pkg/api/errors/validation.go
View file @
b08e5b24
...
...
@@ -80,38 +80,40 @@ type ValidationError struct {
BadValue
interface
{}
}
func
(
v
ValidationError
)
Error
()
string
{
var
_
error
=
&
ValidationError
{}
func
(
v
*
ValidationError
)
Error
()
string
{
return
fmt
.
Sprintf
(
"%s: %v '%v'"
,
v
.
Field
,
ValueOf
(
v
.
Type
),
v
.
BadValue
)
}
// NewFieldRequired returns a ValidationError indicating "value required"
func
NewFieldRequired
(
field
string
,
value
interface
{})
ValidationError
{
return
ValidationError
{
ValidationErrorTypeRequired
,
field
,
value
}
// NewFieldRequired returns a
*
ValidationError indicating "value required"
func
NewFieldRequired
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
ValidationErrorTypeRequired
,
field
,
value
}
}
// NewFieldInvalid returns a ValidationError indicating "invalid value"
func
NewFieldInvalid
(
field
string
,
value
interface
{})
ValidationError
{
return
ValidationError
{
ValidationErrorTypeInvalid
,
field
,
value
}
// NewFieldInvalid returns a
*
ValidationError indicating "invalid value"
func
NewFieldInvalid
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
ValidationErrorTypeInvalid
,
field
,
value
}
}
// NewFieldNotSupported returns a ValidationError indicating "unsupported value"
func
NewFieldNotSupported
(
field
string
,
value
interface
{})
ValidationError
{
return
ValidationError
{
ValidationErrorTypeNotSupported
,
field
,
value
}
// NewFieldNotSupported returns a
*
ValidationError indicating "unsupported value"
func
NewFieldNotSupported
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
ValidationErrorTypeNotSupported
,
field
,
value
}
}
// NewFieldForbidden returns a ValidationError indicating "forbidden"
func
NewFieldForbidden
(
field
string
,
value
interface
{})
ValidationError
{
return
ValidationError
{
ValidationErrorTypeForbidden
,
field
,
value
}
// NewFieldForbidden returns a
*
ValidationError indicating "forbidden"
func
NewFieldForbidden
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
ValidationErrorTypeForbidden
,
field
,
value
}
}
// NewFieldDuplicate returns a ValidationError indicating "duplicate value"
func
NewFieldDuplicate
(
field
string
,
value
interface
{})
ValidationError
{
return
ValidationError
{
ValidationErrorTypeDuplicate
,
field
,
value
}
// NewFieldDuplicate returns a
*
ValidationError indicating "duplicate value"
func
NewFieldDuplicate
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
ValidationErrorTypeDuplicate
,
field
,
value
}
}
// NewFieldNotFound returns a ValidationError indicating "value not found"
func
NewFieldNotFound
(
field
string
,
value
interface
{})
ValidationError
{
return
ValidationError
{
ValidationErrorTypeNotFound
,
field
,
value
}
// NewFieldNotFound returns a
*
ValidationError indicating "value not found"
func
NewFieldNotFound
(
field
string
,
value
interface
{})
*
ValidationError
{
return
&
ValidationError
{
ValidationErrorTypeNotFound
,
field
,
value
}
}
// ValidationErrorList is a collection of ValidationErrors. This does not
...
...
@@ -131,7 +133,7 @@ func (list ValidationErrorList) ToError() error {
// Returns the list for convenience.
func
(
list
ValidationErrorList
)
Prefix
(
prefix
string
)
ValidationErrorList
{
for
i
:=
range
list
{
if
err
,
ok
:=
list
[
i
]
.
(
ValidationError
);
ok
{
if
err
,
ok
:=
list
[
i
]
.
(
*
ValidationError
);
ok
{
if
strings
.
HasPrefix
(
err
.
Field
,
"["
)
{
err
.
Field
=
prefix
+
err
.
Field
}
else
if
len
(
err
.
Field
)
!=
0
{
...
...
pkg/api/errors/validation_test.go
View file @
b08e5b24
...
...
@@ -23,27 +23,27 @@ import (
func
TestMakeFuncs
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
fn
func
()
ValidationError
fn
func
()
*
ValidationError
expected
ValidationErrorType
}{
{
func
()
ValidationError
{
return
NewFieldInvalid
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldInvalid
(
"f"
,
"v"
)
},
ValidationErrorTypeInvalid
,
},
{
func
()
ValidationError
{
return
NewFieldNotSupported
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldNotSupported
(
"f"
,
"v"
)
},
ValidationErrorTypeNotSupported
,
},
{
func
()
ValidationError
{
return
NewFieldDuplicate
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldDuplicate
(
"f"
,
"v"
)
},
ValidationErrorTypeDuplicate
,
},
{
func
()
ValidationError
{
return
NewFieldNotFound
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldNotFound
(
"f"
,
"v"
)
},
ValidationErrorTypeNotFound
,
},
{
func
()
ValidationError
{
return
NewFieldRequired
(
"f"
,
"v"
)
},
func
()
*
ValidationError
{
return
NewFieldRequired
(
"f"
,
"v"
)
},
ValidationErrorTypeRequired
,
},
}
...
...
@@ -65,7 +65,7 @@ func TestValidationError(t *testing.T) {
func
TestErrListPrefix
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
Err
ValidationError
Err
*
ValidationError
Expected
string
}{
{
...
...
@@ -87,7 +87,7 @@ func TestErrListPrefix(t *testing.T) {
if
prefix
==
nil
||
len
(
prefix
)
!=
len
(
errList
)
{
t
.
Errorf
(
"Prefix should return self"
)
}
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
ValidationError
)
.
Field
;
e
!=
a
{
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
*
ValidationError
)
.
Field
;
e
!=
a
{
t
.
Errorf
(
"expected %s, got %s"
,
e
,
a
)
}
}
...
...
@@ -95,7 +95,7 @@ func TestErrListPrefix(t *testing.T) {
func
TestErrListPrefixIndex
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
Err
ValidationError
Err
*
ValidationError
Expected
string
}{
{
...
...
@@ -117,7 +117,7 @@ func TestErrListPrefixIndex(t *testing.T) {
if
prefix
==
nil
||
len
(
prefix
)
!=
len
(
errList
)
{
t
.
Errorf
(
"PrefixIndex should return self"
)
}
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
ValidationError
)
.
Field
;
e
!=
a
{
if
e
,
a
:=
testCase
.
Expected
,
errList
[
0
]
.
(
*
ValidationError
)
.
Field
;
e
!=
a
{
t
.
Errorf
(
"expected %s, got %s"
,
e
,
a
)
}
}
...
...
pkg/api/validation/validation_test.go
View file @
b08e5b24
...
...
@@ -29,7 +29,7 @@ import (
func
expectPrefix
(
t
*
testing
.
T
,
prefix
string
,
errs
errors
.
ValidationErrorList
)
{
for
i
:=
range
errs
{
if
f
,
p
:=
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Field
,
prefix
;
!
strings
.
HasPrefix
(
f
,
p
)
{
if
f
,
p
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
,
prefix
;
!
strings
.
HasPrefix
(
f
,
p
)
{
t
.
Errorf
(
"expected prefix '%s' for field '%s' (%v)"
,
p
,
f
,
errs
[
i
])
}
}
...
...
@@ -69,10 +69,10 @@ func TestValidateVolumes(t *testing.T) {
continue
}
for
i
:=
range
errs
{
if
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Type
!=
v
.
T
{
if
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Type
!=
v
.
T
{
t
.
Errorf
(
"%s: expected errors to have type %s: %v"
,
k
,
v
.
T
,
errs
[
i
])
}
if
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Field
!=
v
.
F
{
if
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
!=
v
.
F
{
t
.
Errorf
(
"%s: expected errors to have field %s: %v"
,
k
,
v
.
F
,
errs
[
i
])
}
}
...
...
@@ -125,10 +125,10 @@ func TestValidatePorts(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
for
i
:=
range
errs
{
if
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Type
!=
v
.
T
{
if
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Type
!=
v
.
T
{
t
.
Errorf
(
"%s: expected errors to have type %s: %v"
,
k
,
v
.
T
,
errs
[
i
])
}
if
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Field
!=
v
.
F
{
if
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
!=
v
.
F
{
t
.
Errorf
(
"%s: expected errors to have field %s: %v"
,
k
,
v
.
F
,
errs
[
i
])
}
}
...
...
@@ -995,7 +995,7 @@ func TestValidateReplicationController(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Field
field
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
if
!
strings
.
HasPrefix
(
field
,
"spec.template."
)
&&
field
!=
"name"
&&
field
!=
"namespace"
&&
...
...
@@ -1078,7 +1078,7 @@ func TestValidateMinion(t *testing.T) {
t
.
Errorf
(
"expected failure for %s"
,
k
)
}
for
i
:=
range
errs
{
field
:=
errs
[
i
]
.
(
errors
.
ValidationError
)
.
Field
field
:=
errs
[
i
]
.
(
*
errors
.
ValidationError
)
.
Field
if
field
!=
"name"
&&
field
!=
"label"
{
t
.
Errorf
(
"%s: missing prefix for: %v"
,
k
,
errs
[
i
])
...
...
pkg/config/config.go
View file @
b08e5b24
...
...
@@ -52,7 +52,7 @@ func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor
}
if
err
:=
CreateObject
(
client
,
mapping
,
obj
);
err
!=
nil
{
reportError
(
&
allErrors
,
i
,
*
err
)
reportError
(
&
allErrors
,
i
,
err
)
}
}
...
...
@@ -65,21 +65,18 @@ func CreateObjects(typer runtime.ObjectTyper, mapper meta.RESTMapper, clientFor
func
CreateObject
(
client
*
client
.
RESTClient
,
mapping
*
meta
.
RESTMapping
,
obj
runtime
.
Object
)
*
errs
.
ValidationError
{
name
,
err
:=
mapping
.
MetadataAccessor
.
Name
(
obj
)
if
err
!=
nil
||
name
==
""
{
e
:=
errs
.
NewFieldRequired
(
"name"
,
err
)
return
&
e
return
errs
.
NewFieldRequired
(
"name"
,
err
)
}
namespace
,
err
:=
mapping
.
Namespace
(
obj
)
if
err
!=
nil
{
e
:=
errs
.
NewFieldRequired
(
"namespace"
,
err
)
return
&
e
return
errs
.
NewFieldRequired
(
"namespace"
,
err
)
}
// TODO: This should be using RESTHelper
err
=
client
.
Post
()
.
Path
(
mapping
.
Resource
)
.
Namespace
(
namespace
)
.
Body
(
obj
)
.
Do
()
.
Error
()
if
err
!=
nil
{
e
:=
errs
.
NewFieldInvalid
(
name
,
err
)
return
&
e
return
errs
.
NewFieldInvalid
(
name
,
err
)
}
return
nil
...
...
@@ -87,7 +84,7 @@ func CreateObject(client *client.RESTClient, mapping *meta.RESTMapping, obj runt
// reportError reports the single item validation error and properly set the
// prefix and index to match the Config item JSON index
func
reportError
(
allErrs
*
errs
.
ValidationErrorList
,
index
int
,
err
errs
.
ValidationError
)
{
func
reportError
(
allErrs
*
errs
.
ValidationErrorList
,
index
int
,
err
*
errs
.
ValidationError
)
{
i
:=
errs
.
ValidationErrorList
{}
*
allErrs
=
append
(
*
allErrs
,
append
(
i
,
err
)
.
PrefixIndex
(
index
)
.
Prefix
(
"item"
)
...
)
}
pkg/config/config_test.go
View file @
b08e5b24
...
...
@@ -92,7 +92,7 @@ func TestCreateNoNameItem(t *testing.T) {
t
.
Errorf
(
"Expected required value error for missing name"
)
}
e
:=
errs
[
0
]
.
(
errors
.
ValidationError
)
e
:=
errs
[
0
]
.
(
*
errors
.
ValidationError
)
if
errors
.
ValueOf
(
e
.
Type
)
!=
"required value"
{
t
.
Errorf
(
"Expected ValidationErrorTypeRequired error, got %#v"
,
e
)
}
...
...
@@ -121,7 +121,7 @@ func TestCreateInvalidItem(t *testing.T) {
t
.
Errorf
(
"Expected invalid value error for kind"
)
}
e
:=
errs
[
0
]
.
(
errors
.
ValidationError
)
e
:=
errs
[
0
]
.
(
*
errors
.
ValidationError
)
if
errors
.
ValueOf
(
e
.
Type
)
!=
"invalid value"
{
t
.
Errorf
(
"Expected ValidationErrorTypeInvalid error, got %#v"
,
e
)
}
...
...
@@ -153,7 +153,7 @@ func TestCreateNoClientItems(t *testing.T) {
t
.
Errorf
(
"Expected invalid value error for client"
)
}
e
:=
errs
[
0
]
.
(
errors
.
ValidationError
)
e
:=
errs
[
0
]
.
(
*
errors
.
ValidationError
)
if
errors
.
ValueOf
(
e
.
Type
)
!=
"unsupported value"
{
t
.
Errorf
(
"Expected ValidationErrorTypeUnsupported error, got %#v"
,
e
)
}
...
...
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