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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
90267f8d
Commit
90267f8d
authored
3 years ago
by
Jan Semmelink
Browse files
Options
Downloads
Patches
Plain Diff
Added HTTP error
parent
43673d55
Branches
1-http-error
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
errors/error.go
+26
-0
26 additions, 0 deletions
errors/error.go
errors/errors.go
+16
-0
16 additions, 0 deletions
errors/errors.go
errors/http-error_test.go
+51
-0
51 additions, 0 deletions
errors/http-error_test.go
with
93 additions
and
0 deletions
errors/error.go
+
26
−
0
View file @
90267f8d
...
...
@@ -2,6 +2,7 @@ package errors
import
(
"fmt"
"net/http"
"path"
"strconv"
"strings"
...
...
@@ -11,6 +12,7 @@ import (
// error
// github.com/pkg/errors: Cause
type
CustomError
struct
{
code
int
message
string
caller
Caller
cause
error
...
...
@@ -26,6 +28,27 @@ func (err CustomError) Cause() error {
return
err
.
cause
}
func
HTTPCode
(
err
error
)
int
{
if
errWithCode
,
ok
:=
err
.
(
ErrorWithCause
);
ok
{
return
errWithCode
.
Code
()
}
return
0
}
func
(
err
CustomError
)
Code
()
int
{
//find http error code - returning the smallest code in the stack of causes (excluding code==0)
code
:=
err
.
code
if
err
.
cause
!=
nil
{
if
causeWithCode
,
ok
:=
err
.
cause
.
(
ErrorWithCause
);
ok
{
causeCode
:=
causeWithCode
.
Code
()
if
code
==
0
||
(
causeCode
!=
0
&&
causeCode
<
code
)
{
code
=
causeCode
}
}
}
return
code
}
func
(
err
CustomError
)
Description
()
Description
{
info
:=
err
.
caller
.
Info
()
desc
:=
&
Description
{
...
...
@@ -77,6 +100,9 @@ func (err CustomError) Formatted(opts FormattingOptions) string {
)
}
thisError
+=
err
.
message
if
err
.
code
!=
0
{
thisError
+=
fmt
.
Sprintf
(
" HTTP(%d:%s)"
,
err
.
code
,
http
.
StatusText
(
err
.
code
))
}
if
!
opts
.
Causes
{
return
thisError
}
...
...
This diff is collapsed.
Click to expand it.
errors/errors.go
+
16
−
0
View file @
90267f8d
...
...
@@ -10,6 +10,7 @@ import (
type
ErrorWithCause
interface
{
error
Cause
()
error
Code
()
int
}
func
New
(
message
string
)
error
{
...
...
@@ -67,6 +68,21 @@ func Wrap(err error, msg string) error {
return
wrappedErr
}
func
HTTP
(
code
int
,
err
error
,
format
string
,
args
...
interface
{})
error
{
if
err
==
nil
{
return
nil
}
wrappedErr
:=
&
CustomError
{
code
:
code
,
message
:
fmt
.
Sprintf
(
format
,
args
...
),
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 → 100644
+
51
−
0
View file @
90267f8d
package
errors_test
import
(
"net/http"
"testing"
"gitlab.com/uafrica/go-utils/errors"
)
func
TestHTTPError
(
t
*
testing
.
T
)
{
var
err
error
//you can wrap any error with an HTTP code:
err
=
errors
.
Errorf
(
"failed to connect to db"
)
//err = errors.HTTP(http.StatusInternalServerError, err)
//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"
)
//now log:
t
.
Logf
(
"failed:
\n\t
%+v"
,
err
)
t
.
Logf
(
"HTTP Code: %d"
,
errors
.
HTTPCode
(
err
))
//will return smallest code in the stack, 0 if none.
//you can wrap any error with an HTTP code:
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"
)
//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"
)
//now log:
t
.
Logf
(
"failed:
\n\t
%+v"
,
err
)
t
.
Logf
(
"HTTP Code: %d"
,
errors
.
HTTPCode
(
err
))
//will return smallest code in the stack, 0 if none.
err
=
errors
.
Errorf
(
"failed to connect to db"
)
err
=
errors
.
Wrapf
(
err
,
"failed to connect to db"
)
t
.
Logf
(
"failed:
\n\t
%+v"
,
err
)
t
.
Logf
(
"HTTP Code: %d"
,
errors
.
HTTPCode
(
err
))
//will return smallest code in the stack, 0 if none.
}
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