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
d4e877ef
Commit
d4e877ef
authored
3 years ago
by
Cornel Rautenbach
Browse files
Options
Downloads
Patches
Plain Diff
HTTP and error changes
parent
d14e15cc
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
errors/error.go
+13
-10
13 additions, 10 deletions
errors/error.go
errors/errors.go
+24
-0
24 additions, 0 deletions
errors/errors.go
errors/http_error_test.go
+0
-6
0 additions, 6 deletions
errors/http_error_test.go
with
37 additions
and
16 deletions
errors/error.go
+
13
−
10
View file @
d4e877ef
...
...
@@ -118,21 +118,24 @@ func (err CustomError) Formatted(opts FormattingOptions) string {
err
.
caller
.
Function
(),
)
}
thisError
+=
err
.
message
if
!
opts
.
Causes
{
return
thisError
if
err
.
cause
==
nil
||
!
opts
.
Causes
{
return
err
.
message
}
if
err
.
cause
==
nil
{
return
thisError
if
err
.
cause
.
Error
()
!=
err
.
message
{
thisError
+=
err
.
message
}
sep
:=
", because"
sep
:=
""
if
len
(
thisError
)
>
0
{
sep
=
", because"
if
opts
.
NewLines
{
sep
+=
"
\n\t
"
}
else
{
sep
+=
" "
}
}
if
causeWithStack
,
ok
:=
err
.
cause
.
(
*
CustomError
);
ok
{
return
thisError
+
sep
+
causeWithStack
.
Formatted
(
opts
)
...
...
This diff is collapsed.
Click to expand it.
errors/errors.go
+
24
−
0
View file @
d4e877ef
...
...
@@ -88,6 +88,30 @@ func HTTP(code int, err error, format string, args ...interface{}) error {
return
wrappedErr
}
func
HTTPWithMsg
(
code
int
,
format
string
,
args
...
interface
{})
error
{
errorString
:=
fmt
.
Sprintf
(
format
,
args
...
)
wrappedErr
:=
&
CustomError
{
code
:
code
,
message
:
errorString
,
caller
:
GetCaller
(
2
),
cause
:
Error
(
errorString
),
}
return
wrappedErr
}
func
HTTPWithError
(
code
int
,
err
error
)
error
{
wrappedErr
:=
&
CustomError
{
code
:
code
,
message
:
err
.
Error
(),
caller
:
GetCaller
(
2
),
cause
:
err
,
}
return
wrappedErr
}
type
Description
struct
{
Message
string
`json:"error"`
Source
*
CallerInfo
`json:"source,omitempty"`
...
...
This diff is collapsed.
Click to expand it.
errors/http_error_test.go
+
0
−
6
View file @
d4e877ef
...
...
@@ -17,9 +17,6 @@ func TestHTTPError(t *testing.T) {
//or if you know you are creating the HTTP error, do it as one statement
err
=
errors
.
HTTP
(
http
.
StatusBadRequest
,
err
,
"failed to get user"
)
//and one more to give a lower code again
err
=
errors
.
HTTP
(
http
.
StatusInsufficientStorage
,
err
,
"jissis this is bad!"
)
//and higher again -... many layers...
err
=
errors
.
HTTP
(
http
.
StatusNotFound
,
err
,
"terrible mistake"
)
...
...
@@ -31,9 +28,6 @@ func TestHTTPError(t *testing.T) {
err
=
errors
.
Errorf
(
"failed to connect to db"
)
//err = errors.HTTP(http.StatusInternalServerError, err)
//and one more to give a lower code again
err
=
errors
.
HTTP
(
http
.
StatusInsufficientStorage
,
err
,
"jissis this is bad!"
)
//and higher again -... many layers...
err
=
errors
.
HTTP
(
http
.
StatusNotFound
,
err
,
"terrible mistake"
)
...
...
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