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
d7a4bf73
Commit
d7a4bf73
authored
3 years ago
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Renamed Error to CustomError and created Error() func to be similar to Wrap() without formatting
parent
a42306d9
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
errors/error.go
+10
-11
10 additions, 11 deletions
errors/error.go
errors/error_formats_test.go
+2
-2
2 additions, 2 deletions
errors/error_formats_test.go
errors/errors.go
+9
-12
9 additions, 12 deletions
errors/errors.go
errors/errors_test.go
+1
-1
1 addition, 1 deletion
errors/errors_test.go
with
22 additions
and
26 deletions
errors/error.go
+
10
−
11
View file @
d7a4bf73
...
...
@@ -7,34 +7,33 @@ import (
"strings"
)
//Error implements the following interfaces:
//
Custom
Error implements the following interfaces:
// error
// github.com/pkg/errors: Cause
type
Error
struct
{
type
Custom
Error
struct
{
message
string
caller
Caller
//stack []CallerInfo
cause
error
}
//implement interface error:
func
(
err
Error
)
Error
()
string
{
func
(
err
Custom
Error
)
Error
()
string
{
return
err
.
Formatted
(
FormattingOptions
{
Causes
:
true
})
}
//implement github.com/pkg/errors: Cause
func
(
err
Error
)
Cause
()
error
{
func
(
err
Custom
Error
)
Cause
()
error
{
return
err
.
cause
}
func
(
err
Error
)
Description
()
Description
{
func
(
err
Custom
Error
)
Description
()
Description
{
info
:=
err
.
caller
.
Info
()
desc
:=
&
Description
{
Message
:
err
.
message
,
Source
:
&
info
,
}
if
err
.
cause
!=
nil
{
causeWithStack
,
ok
:=
err
.
cause
.
(
*
Error
)
causeWithStack
,
ok
:=
err
.
cause
.
(
*
Custom
Error
)
if
!
ok
{
//external cause without our stack
//if github.com/pkg/errors, we can still get caller reference
...
...
@@ -48,7 +47,7 @@ func (err Error) Description() Description {
return
*
desc
}
func
(
err
Error
)
Format
(
s
fmt
.
State
,
v
rune
)
{
func
(
err
Custom
Error
)
Format
(
s
fmt
.
State
,
v
rune
)
{
s
.
Write
([]
byte
(
err
.
Formatted
(
FormattingOptions
{
...
...
@@ -66,7 +65,7 @@ type FormattingOptions struct {
Source
bool
}
func
(
err
Error
)
Formatted
(
opts
FormattingOptions
)
string
{
func
(
err
Custom
Error
)
Formatted
(
opts
FormattingOptions
)
string
{
//start with this error
thisError
:=
""
if
opts
.
Source
{
...
...
@@ -93,7 +92,7 @@ func (err Error) Formatted(opts FormattingOptions) string {
sep
+=
" "
}
if
causeWithStack
,
ok
:=
err
.
cause
.
(
*
Error
);
ok
{
if
causeWithStack
,
ok
:=
err
.
cause
.
(
*
Custom
Error
);
ok
{
return
thisError
+
sep
+
causeWithStack
.
Formatted
(
opts
)
}
...
...
This diff is collapsed.
Click to expand it.
errors/error_formats_test.go
+
2
−
2
View file @
d7a4bf73
...
...
@@ -265,13 +265,13 @@ func show(t *testing.T, title string, err error) {
t
.
Logf
(
"------------------------------------------------------------------------------------"
)
t
.
Logf
(
"err.Error(): %s"
,
err
.
Error
())
t
.
Logf
(
"Printf(%%s): %s"
,
err
)
if
_
,
ok
:=
err
.
(
*
uf_errors
.
Error
);
ok
{
//compact only supported on our own lib:
if
_
,
ok
:=
err
.
(
*
uf_errors
.
Custom
Error
);
ok
{
//compact only supported on our own lib:
t
.
Logf
(
"Printf(%%c): %c"
,
err
)
t
.
Logf
(
"Printf(%%+c): %+c"
,
err
)
}
t
.
Logf
(
"Printf(%%v): %v"
,
err
)
t
.
Logf
(
"Printf(%%+v): %+v"
,
err
)
if
e
,
ok
:=
err
.
(
*
uf_errors
.
Error
);
ok
{
//description only supported on our own lib:
if
e
,
ok
:=
err
.
(
*
uf_errors
.
Custom
Error
);
ok
{
//description only supported on our own lib:
desc
:=
e
.
Description
()
jsonDesc
,
_
:=
json
.
Marshal
(
desc
)
t
.
Logf
(
"JSON: %s"
,
string
(
jsonDesc
))
...
...
This diff is collapsed.
Click to expand it.
errors/errors.go
+
9
−
12
View file @
d7a4bf73
...
...
@@ -13,30 +13,27 @@ type ErrorWithCause interface {
}
func
New
(
message
string
)
error
{
err
:=
&
Error
{
err
:=
&
Custom
Error
{
message
:
message
,
caller
:
GetCaller
(
2
),
//stack: Stack(3),
cause
:
nil
,
}
return
err
}
func
N
Error
(
message
string
)
error
{
err
:=
&
Error
{
func
Error
(
message
string
)
error
{
err
:=
&
Custom
Error
{
message
:
message
,
caller
:
GetCaller
(
2
),
//stack: Stack(3),
cause
:
nil
,
}
return
err
}
func
Errorf
(
format
string
,
args
...
interface
{})
error
{
err
:=
&
Error
{
err
:=
&
Custom
Error
{
message
:
fmt
.
Sprintf
(
format
,
args
...
),
caller
:
GetCaller
(
2
),
//stack: Stack(3),
cause
:
nil
,
}
return
err
...
...
@@ -47,7 +44,7 @@ func Wrapf(err error, format string, args ...interface{}) error {
return
nil
}
wrappedErr
:=
&
Error
{
wrappedErr
:=
&
Custom
Error
{
message
:
fmt
.
Sprintf
(
format
,
args
...
),
caller
:
GetCaller
(
2
),
cause
:
err
,
...
...
@@ -61,7 +58,7 @@ func Wrap(err error, msg string) error {
return
nil
}
wrappedErr
:=
&
Error
{
wrappedErr
:=
&
Custom
Error
{
message
:
msg
,
caller
:
GetCaller
(
2
),
cause
:
err
,
...
...
This diff is collapsed.
Click to expand it.
errors/errors_test.go
+
1
−
1
View file @
d7a4bf73
...
...
@@ -14,7 +14,7 @@ func TestErrorFormatting(t *testing.T) {
e1
:=
errors
.
Errorf
(
"you have problem in your SQL near xxx"
)
e2
:=
errors
.
Wrapf
(
e1
,
"query failed"
)
e3
:=
errors
.
Wrapf
(
e2
,
"failed to find account"
)
e4
:=
errors
.
Wrapf
(
e3
,
"login failed"
)
.
(
*
errors
.
Error
)
e4
:=
errors
.
Wrapf
(
e3
,
"login failed"
)
.
(
*
errors
.
Custom
Error
)
//we can log the error in different ways:
...
...
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