Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
accountserver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
accountserver
Commits
6273a767
Commit
6273a767
authored
5 months ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Fix isCriticalError() check
parent
20150213
No related branches found
No related tags found
1 merge request
!142
Refactor validation code for readability
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
errors_test.go
+69
-0
69 additions, 0 deletions
errors_test.go
validation_errors.go
+20
-7
20 additions, 7 deletions
validation_errors.go
with
89 additions
and
7 deletions
errors_test.go
+
69
−
0
View file @
6273a767
package
accountserver
package
accountserver
import
(
import
(
"errors"
"testing"
"testing"
)
)
...
@@ -14,6 +15,74 @@ func TestErrors(t *testing.T) {
...
@@ -14,6 +15,74 @@ func TestErrors(t *testing.T) {
if
!
IsValidationError
(
rerr
)
{
if
!
IsValidationError
(
rerr
)
{
t
.
Errorf
(
"IsValidationError() returned false"
)
t
.
Errorf
(
"IsValidationError() returned false"
)
}
}
if
!
IsValidationError
(
errors
.
Join
(
newValidationErrorStr
(
"field1"
,
"bad"
),
newValidationErrorStr
(
"field2"
,
"bad"
),
))
{
t
.
Errorf
(
"IsValidationError() does not survive aggregation"
)
}
}
func
TestErrors_Criticality
(
t
*
testing
.
T
)
{
for
_
,
isAdmin
:=
range
[]
bool
{
false
,
true
}
{
for
idx
,
td
:=
range
[]
struct
{
err
error
crit
bool
}{
{
errors
.
New
(
"bare error"
),
true
},
{
newValidationErrorStr
(
"field"
,
"bad"
),
true
},
{
nonCriticalErr
(
newValidationErrorStr
(
"field"
,
"bad"
)),
false
},
{
errors
.
Join
(
newValidationErrorStr
(
"field1"
,
"bad"
),
newValidationErrorStr
(
"field2"
,
"bad"
),
),
true
},
{
errors
.
Join
(
newValidationErrorStr
(
"field1"
,
"bad"
),
nonCriticalErr
(
newValidationErrorStr
(
"field2"
,
"bad"
)),
),
true
},
{
errors
.
Join
(
nonCriticalErr
(
newValidationErrorStr
(
"field1"
,
"bad"
)),
nonCriticalErr
(
newValidationErrorStr
(
"field2"
,
"bad"
)),
),
false
},
}
{
rctx
:=
&
RequestContext
{
Auth
:
Auth
{
IsAdmin
:
isAdmin
},
}
crit
:=
isCriticalErr
(
rctx
,
td
.
err
)
// All errors are critical for non-admins.
expectCrit
:=
td
.
crit
if
!
isAdmin
{
expectCrit
=
true
}
if
crit
!=
expectCrit
{
t
.
Errorf
(
"[%d] test failed, crit=%v expected=%v"
,
idx
,
crit
,
expectCrit
)
}
}
}
}
func
TestErrors_CriticalMasking
(
t
*
testing
.
T
)
{
rctx
:=
&
RequestContext
{
Auth
:
Auth
{
IsAdmin
:
true
},
}
err
:=
errors
.
Join
(
newValidationErrorStr
(
"field1"
,
"bad"
),
nonCriticalErr
(
newValidationErrorStr
(
"field2"
,
"not too bad"
)),
)
if
!
isCriticalErr
(
rctx
,
err
)
{
t
.
Fatal
(
"critical error was masked"
)
}
err
=
errors
.
Join
(
nonCriticalErr
(
newValidationErrorStr
(
"field1"
,
"not too bad"
)),
nonCriticalErr
(
newValidationErrorStr
(
"field2"
,
"not too bad"
)),
)
if
isCriticalErr
(
rctx
,
err
)
{
t
.
Fatal
(
"non-critical error status was lost in aggregation"
)
}
}
}
func
TestErrors_JSON
(
t
*
testing
.
T
)
{
func
TestErrors_JSON
(
t
*
testing
.
T
)
{
...
...
This diff is collapsed.
Click to expand it.
validation_errors.go
+
20
−
7
View file @
6273a767
package
accountserver
package
accountserver
import
"errors"
// Some validation errors are non-critical, i.e. they can be safely
// Some validation errors are non-critical, i.e. they can be safely
// overridden if the request is made by an administrator.
// overridden if the request is made by an administrator.
type
nonCriticalValidationError
struct
{
type
nonCriticalValidationError
struct
{
...
@@ -21,11 +19,6 @@ func nonCriticalErr(err error) error {
...
@@ -21,11 +19,6 @@ func nonCriticalErr(err error) error {
return
&
nonCriticalValidationError
{
wrap
:
err
}
return
&
nonCriticalValidationError
{
wrap
:
err
}
}
}
func
isCriticalErr
(
rctx
*
RequestContext
,
err
error
)
bool
{
return
!
(
errors
.
Is
(
err
,
&
nonCriticalValidationError
{})
&&
rctx
.
Auth
.
IsAdmin
)
}
func
nonCritical
(
f
ValidatorFunc
)
ValidatorFunc
{
func
nonCritical
(
f
ValidatorFunc
)
ValidatorFunc
{
return
func
(
rctx
*
RequestContext
,
s
string
)
error
{
return
func
(
rctx
*
RequestContext
,
s
string
)
error
{
if
err
:=
f
(
rctx
,
s
);
err
!=
nil
{
if
err
:=
f
(
rctx
,
s
);
err
!=
nil
{
...
@@ -34,3 +27,23 @@ func nonCritical(f ValidatorFunc) ValidatorFunc {
...
@@ -34,3 +27,23 @@ func nonCritical(f ValidatorFunc) ValidatorFunc {
return
nil
return
nil
}
}
}
}
func
unwrapCriticalErrs
(
err
error
)
bool
{
if
_
,
ok
:=
err
.
(
*
nonCriticalValidationError
);
ok
{
return
false
}
else
if
t
,
ok
:=
err
.
(
wrappedErrorMulti
);
ok
{
for
_
,
innerErr
:=
range
t
.
Unwrap
()
{
if
crit
:=
unwrapCriticalErrs
(
innerErr
);
crit
{
return
true
}
}
return
false
}
else
if
t
,
ok
:=
err
.
(
wrappedError
);
ok
{
return
unwrapCriticalErrs
(
t
.
Unwrap
())
}
return
true
}
func
isCriticalErr
(
rctx
*
RequestContext
,
err
error
)
bool
{
return
!
(
rctx
.
Auth
.
IsAdmin
&&
!
unwrapCriticalErrs
(
err
))
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment