Skip to content

Commit 0de0aac

Browse files
author
JojiiOfficial
committed
improve error handling
Update dependencies
1 parent 2377fd7 commit 0de0aac

15 files changed

Lines changed: 365 additions & 298 deletions

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module github.com/DataManager-Go/DataManagerServer
33
go 1.14
44

55
require (
6-
github.com/DataManager-Go/libdatamanager v1.2.9
6+
github.com/DataManager-Go/libdatamanager v1.3.0
77
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28
8-
github.com/JojiiOfficial/gaw v1.2.6
8+
github.com/JojiiOfficial/gaw v1.2.8
99
github.com/JojiiOfficial/shred v1.2.1
1010
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
1111
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ github.com/DataManager-Go/libdatamanager v1.2.7 h1:qwHmroP5vW4qZu7NX1tM80sYv4puN
1616
github.com/DataManager-Go/libdatamanager v1.2.7/go.mod h1:QHVsduLyGDmvSh+OG5XUgijhjTBfWV+nP9zocWY7zt0=
1717
github.com/DataManager-Go/libdatamanager v1.2.9 h1:zdQbhJ5Qoq9qq2NlP3FzFreo7O4IpFFTX49gdvBgk3k=
1818
github.com/DataManager-Go/libdatamanager v1.2.9/go.mod h1:q3Nxbk8mvaiT9HRgRd0QkAPuh5mtAVrki7sTTaIbxCY=
19+
github.com/DataManager-Go/libdatamanager v1.3.0 h1:0WvHBwPgzX5GFsKYvMoF8WRgQJfr0+lBwI4e0CU+Exc=
20+
github.com/DataManager-Go/libdatamanager v1.3.0/go.mod h1:8QzTM9YaJte35gQJ6xZNgQdoyYwBKK6KUtdibrqDXiM=
1921
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28 h1:nYoIExG+Z/gSLS9Jbpu6lnrh+m6e9gTxQfGhanTsExE=
2022
github.com/JojiiOfficial/configService v0.0.0-20200219132202-6e71512e2e28/go.mod h1:j1kHFoYWAbLRPE5nyAAtODwUc0xwd2+ifPZ3uCAgv/g=
2123
github.com/JojiiOfficial/gaw v1.2.1 h1:zwVLf5TQrXQF+pwI+/sVsfxTxmAZbZ5BjVT/nKxb2Ck=
@@ -24,6 +26,8 @@ github.com/JojiiOfficial/gaw v1.2.5 h1:wbguzCqNaCBMYH1a9D6EHW2aIG21uo6CS6qaHLi5I
2426
github.com/JojiiOfficial/gaw v1.2.5/go.mod h1:Y0hrpN0iX0L5bBf/8+kIER7R/m4GTNuKkifXisMG4S4=
2527
github.com/JojiiOfficial/gaw v1.2.6 h1:JU8GDvykk2IG6Kevw5aVqTuRO+kbpZXZ8yBfVgTU0t0=
2628
github.com/JojiiOfficial/gaw v1.2.6/go.mod h1:Y0hrpN0iX0L5bBf/8+kIER7R/m4GTNuKkifXisMG4S4=
29+
github.com/JojiiOfficial/gaw v1.2.8 h1:crLd2hrRvTlCClZDtwqnr8AoVKs3uQAk8B2AdOfUCsg=
30+
github.com/JojiiOfficial/gaw v1.2.8/go.mod h1:fPm2wG1z8xSCmfkqq9V5iHdlgLUpkRx73tSO9efhJP0=
2731
github.com/JojiiOfficial/shred v1.2.1 h1:658CFVTqcAkYVg815vW+guYnyJTLOIoS15tMyPTYhNo=
2832
github.com/JojiiOfficial/shred v1.2.1/go.mod h1:/OAxd6eYOhrXb3KW+2wmDog2BiFlUld8oJEKa+xblxU=
2933
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafoB+tBA3gMyHYHrpOtNuDiK/uB5uXxq5wM=

handlers/Errors.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package handlers
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
)
7+
8+
var (
9+
// RErrNotFound request error if something
10+
// was requested but wasn't found
11+
RErrNotFound = NewRequestError("not found", http.StatusNotFound)
12+
13+
// RErrAlreadyExists request error if something
14+
// requested already exists
15+
RErrAlreadyExists = NewRequestError("already exists", http.StatusBadRequest)
16+
17+
// RErrBadRequest if a request was bad
18+
RErrBadRequest = NewRequestError("Bad request", http.StatusBadRequest)
19+
20+
// RErrInvalid if something is invalid
21+
RErrInvalid = NewRequestError("invalid", http.StatusUnprocessableEntity)
22+
23+
// RErrTokenInvalid if a token is not valid
24+
RErrTokenInvalid = RErrInvalid.Prepend("Token").WithCode(http.StatusUnauthorized)
25+
26+
// RErrNotSupported if an action is not supported
27+
RErrNotSupported = NewRequestError("not supported", http.StatusUnprocessableEntity)
28+
29+
// RErrNotAllowed if a request is not allowed for a given user
30+
RErrNotAllowed = NewRequestError("not allowed", http.StatusForbidden)
31+
32+
// RErrMissing if something required is missing
33+
RErrMissing = NewRequestError("missing", http.StatusUnprocessableEntity)
34+
35+
// RErrTimeout timeout error
36+
RErrTimeout = NewRequestError("timeout", http.StatusRequestTimeout)
37+
38+
// RErrPermissionDenied if a user has no permission to run a certain command
39+
RErrPermissionDenied = NewRequestError("permission denied", http.StatusForbidden)
40+
)
41+
42+
// RequestError error appearing in a request
43+
type RequestError struct {
44+
Message string
45+
ResponseCode int
46+
}
47+
48+
// NewRequestError create a new requestErorr
49+
func NewRequestError(msg string, code int) *RequestError {
50+
return &RequestError{
51+
Message: msg,
52+
ResponseCode: code,
53+
}
54+
}
55+
56+
// Prepend text to the error
57+
func (re *RequestError) Prepend(txt string) *RequestError {
58+
// Check if space has to be added
59+
if strings.HasSuffix(txt, " ") || strings.HasPrefix(re.Message, " ") {
60+
re.Message = txt + re.Message
61+
} else {
62+
re.Message = txt + " " + re.Message
63+
}
64+
65+
return re
66+
}
67+
68+
// Append text to the error
69+
func (re *RequestError) Append(txt string) *RequestError {
70+
// Check if space has to be added
71+
if strings.HasSuffix(re.Message, " ") || strings.HasPrefix(txt, " ") {
72+
re.Message += txt
73+
} else {
74+
re.Message += " " + txt
75+
}
76+
77+
return re
78+
}
79+
80+
// WithCode uses a different HTTP responsecode
81+
func (re *RequestError) WithCode(code int) *RequestError {
82+
re.ResponseCode = code
83+
return re
84+
}
85+
86+
// Implement String for error
87+
func (re RequestError) String() string {
88+
return re.Message
89+
}
90+
91+
func (re RequestError) Error() string {
92+
return re.String()
93+
}

0 commit comments

Comments
 (0)