/
handle.go
81 lines (69 loc) · 1.8 KB
/
handle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package httpx
import (
"net/http"
"strings"
uuid "github.com/satori/go.uuid"
)
var (
handleGetECodeSuccess func() int
handleGetECodeBadRequest func() int
handleGetECodeInternalError func() int
handleErrToECode func(error) int
handleErrToHttpStatus func(error) int
handleECodeToStr func(int) string
handleNewRequestId func() string
)
func init() {
handleGetECodeSuccess = func() int {
return http.StatusOK
}
handleGetECodeBadRequest = func() int {
return http.StatusBadRequest
}
handleGetECodeInternalError = func() int {
return http.StatusInternalServerError
}
handleErrToECode = func(err error) int {
if err == nil {
return handleGetECodeSuccess()
}
return handleGetECodeInternalError()
}
handleErrToHttpStatus = handleErrToECode
handleECodeToStr = func(status int) string {
str := http.StatusText(status)
if len(str) == 0 {
str = "Unknown"
}
return str
}
handleNewRequestId = func() string {
return strings.ToUpper(uuid.NewV4().String())
}
}
func RegisterHandle(funcGetECodeSuccess, funcGetECodeInternalError, funcGetECodeBadRequest func() int,
funcErrToECode, funcErrToHttpStatus func(error) int,
funcECodeToStr func(int) string,
funcNewRequestId func() string) {
if funcGetECodeSuccess != nil {
handleGetECodeSuccess = funcGetECodeSuccess
}
if funcGetECodeInternalError != nil {
handleGetECodeInternalError = funcGetECodeInternalError
}
if funcGetECodeBadRequest != nil {
handleGetECodeBadRequest = funcGetECodeBadRequest
}
if funcErrToECode != nil {
handleErrToECode = funcErrToECode
}
if funcErrToHttpStatus != nil {
handleErrToHttpStatus = funcErrToHttpStatus
}
if funcECodeToStr != nil {
handleECodeToStr = funcECodeToStr
}
if funcNewRequestId != nil {
handleNewRequestId = funcNewRequestId
}
}