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
c81dda5d
Commit
c81dda5d
authored
Jan 09, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve validation error message printing
parent
4231f875
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
6 deletions
+36
-6
validation.go
pkg/api/errors/validation.go
+4
-3
validation_test.go
pkg/api/errors/validation_test.go
+32
-3
No files found.
pkg/api/errors/validation.go
View file @
c81dda5d
...
@@ -20,6 +20,7 @@ import (
...
@@ -20,6 +20,7 @@ import (
"fmt"
"fmt"
"strings"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/golang/glog"
"github.com/golang/glog"
)
)
...
@@ -51,8 +52,8 @@ const (
...
@@ -51,8 +52,8 @@ const (
ValidationErrorTypeForbidden
ValidationErrorType
=
"FieldValueForbidden"
ValidationErrorTypeForbidden
ValidationErrorType
=
"FieldValueForbidden"
)
)
//
ValueOf
converts a ValidationErrorType into its corresponding error message.
//
String
converts a ValidationErrorType into its corresponding error message.
func
ValueOf
(
t
ValidationErrorType
)
string
{
func
(
t
ValidationErrorType
)
String
(
)
string
{
switch
t
{
switch
t
{
case
ValidationErrorTypeNotFound
:
case
ValidationErrorTypeNotFound
:
return
"not found"
return
"not found"
...
@@ -83,7 +84,7 @@ type ValidationError struct {
...
@@ -83,7 +84,7 @@ type ValidationError struct {
var
_
error
=
&
ValidationError
{}
var
_
error
=
&
ValidationError
{}
func
(
v
*
ValidationError
)
Error
()
string
{
func
(
v
*
ValidationError
)
Error
()
string
{
s
:=
fmt
.
Sprintf
(
"%s: %s '%v'"
,
v
.
Field
,
ValueOf
(
v
.
Type
)
,
v
.
BadValue
)
s
:=
spew
.
Sprintf
(
"%s: %s '%+v'"
,
v
.
Field
,
v
.
Type
,
v
.
BadValue
)
if
v
.
Detail
!=
""
{
if
v
.
Detail
!=
""
{
s
+=
fmt
.
Sprintf
(
": %s"
,
v
.
Detail
)
s
+=
fmt
.
Sprintf
(
": %s"
,
v
.
Detail
)
}
}
...
...
pkg/api/errors/validation_test.go
View file @
c81dda5d
...
@@ -56,10 +56,39 @@ func TestMakeFuncs(t *testing.T) {
...
@@ -56,10 +56,39 @@ func TestMakeFuncs(t *testing.T) {
}
}
}
}
func
TestValidationError
(
t
*
testing
.
T
)
{
func
TestValidationError
UsefulMessage
(
t
*
testing
.
T
)
{
s
:=
NewFieldInvalid
(
"foo"
,
"bar"
,
"deet"
)
.
Error
()
s
:=
NewFieldInvalid
(
"foo"
,
"bar"
,
"deet"
)
.
Error
()
if
!
strings
.
Contains
(
s
,
"foo"
)
||
!
strings
.
Contains
(
s
,
"bar"
)
||
!
strings
.
Contains
(
s
,
"deet"
)
||
!
strings
.
Contains
(
s
,
ValueOf
(
ValidationErrorTypeInvalid
))
{
t
.
Logf
(
"message: %v"
,
s
)
t
.
Errorf
(
"error message did not contain expected values, got %s"
,
s
)
for
_
,
part
:=
range
[]
string
{
"foo"
,
"bar"
,
"deet"
,
ValidationErrorTypeInvalid
.
String
()}
{
if
!
strings
.
Contains
(
s
,
part
)
{
t
.
Errorf
(
"error message did not contain expected part '%v'"
,
part
)
}
}
type
complicated
struct
{
Baz
int
Qux
string
Inner
interface
{}
KV
map
[
string
]
int
}
s
=
NewFieldRequired
(
"foo"
,
&
complicated
{
Baz
:
1
,
Qux
:
"aoeu"
,
Inner
:
&
complicated
{
Qux
:
"asdf"
},
KV
:
map
[
string
]
int
{
"Billy"
:
2
},
},
)
.
Error
()
t
.
Logf
(
"message: %v"
,
s
)
for
_
,
part
:=
range
[]
string
{
"foo"
,
ValidationErrorTypeRequired
.
String
(),
"Baz"
,
"Qux"
,
"Inner"
,
"KV"
,
"1"
,
"aoeu"
,
"asdf"
,
"Billy"
,
"2"
,
}
{
if
!
strings
.
Contains
(
s
,
part
)
{
t
.
Errorf
(
"error message did not contain expected part '%v'"
,
part
)
}
}
}
}
}
...
...
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