Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Bob Public Utils
bobgroup-go-utils
Commits
34f2caf5
Commit
34f2caf5
authored
Sep 17, 2021
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Added errors.Is()
parent
1a9fcd62
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
errors/error.go
+21
-1
21 additions, 1 deletion
errors/error.go
errors/errors.go
+5
-0
5 additions, 0 deletions
errors/errors.go
errors/errors_test.go
+40
-0
40 additions, 0 deletions
errors/errors_test.go
with
66 additions
and
1 deletion
errors/error.go
+
21
−
1
View file @
34f2caf5
...
...
@@ -20,7 +20,27 @@ type CustomError struct {
//implement interface error:
func
(
err
CustomError
)
Error
()
string
{
return
err
.
Formatted
(
FormattingOptions
{
Causes
:
true
})
return
err
.
Formatted
(
FormattingOptions
{
Causes
:
false
})
}
func
Is
(
e1
,
e2
error
)
bool
{
if
e1WithIs
,
ok
:=
e1
.
(
ErrorWithIs
);
ok
{
return
e1WithIs
.
Is
(
e2
)
}
return
e1
.
Error
()
==
e2
.
Error
()
}
//Is() compares the message string of this or any cause to match the specified error message
func
(
err
CustomError
)
Is
(
specificError
error
)
bool
{
if
err
.
message
==
specificError
.
Error
()
{
return
true
}
if
err
.
cause
!=
nil
{
if
causeWithIs
,
ok
:=
err
.
cause
.
(
ErrorWithIs
);
ok
{
return
causeWithIs
.
Is
(
specificError
)
}
}
return
false
}
//implement github.com/pkg/errors: Cause
...
...
This diff is collapsed.
Click to expand it.
errors/errors.go
+
5
−
0
View file @
34f2caf5
...
...
@@ -13,6 +13,11 @@ type ErrorWithCause interface {
Code
()
int
}
type
ErrorWithIs
interface
{
error
Is
(
specificError
error
)
bool
}
func
New
(
message
string
)
error
{
err
:=
&
CustomError
{
message
:
message
,
...
...
This diff is collapsed.
Click to expand it.
errors/errors_test.go
+
40
−
0
View file @
34f2caf5
...
...
@@ -2,6 +2,7 @@ package errors_test
import
(
"encoding/json"
"fmt"
"testing"
"gitlab.com/uafrica/go-utils/errors"
...
...
@@ -90,3 +91,42 @@ func f(i int) error {
}
return
errors
.
Errorf
(
"i=%d is odd"
,
i
)
}
func
TestIs
(
t
*
testing
.
T
)
{
//in some condition program returns
for
n
:=
0
;
n
<=
4
;
n
++
{
if
err
:=
ReadDb
(
n
);
err
!=
nil
{
switch
{
case
errors
.
Is
(
err
,
errNotFound
)
:
t
.
Logf
(
"n=%d failed with NOT FOUND"
,
n
)
case
errors
.
Is
(
err
,
errDisabled
)
:
t
.
Logf
(
"n=%d failed because disabled"
,
n
)
default
:
t
.
Logf
(
"n=%d failed for unknown cause: %v"
,
n
,
err
)
}
}
else
{
t
.
Logf
(
"n=%d worked"
,
n
)
}
}
}
var
(
errNotFound
=
errors
.
New
(
"Not Found"
)
errDisabled
=
errors
.
New
(
"Disabled"
)
)
func
ReadDb
(
x
int
)
error
{
switch
x
{
case
1
:
return
errNotFound
case
2
:
return
errDisabled
case
3
:
return
nil
case
4
:
return
fmt
.
Errorf
(
"some silly bug"
)
default
:
return
errors
.
Errorf
(
"invalid x=%d"
,
x
)
}
}
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
sign in
to comment