-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
215 lines (196 loc) · 7.05 KB
/
response.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package response
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"strconv"
"github.com/ariefaprilianto/ddog-experimental/lib/common/custerr"
"github.com/ariefaprilianto/ddog-experimental/lib/common/err"
)
var (
ErrBadRequest = errors.New("Bad request")
ErrForbiddenResource = errors.New("Forbidden resource")
ErrForbiddenNakama = errors.New("Terms and conditions not accepted")
ErrNotFound = errors.New("Not Found")
ErrPreConditionFailed = errors.New("Precondition failed")
ErrInternalServerError = errors.New("Internal server error")
ErrTimeoutError = errors.New("Timeout error")
ErrAlreadyRegistered = errors.New("User already registered")
ErrNoLinkerExists = errors.New("No Linker exist")
ErrNoValidUserFound = errors.New("No Valid User Found")
ErrTxnAlreadyDone = errors.New("Transaction Already Done")
ErrExcessRefundAmount = errors.New("Excess Refund Amount")
ErrRefundAlreadyDone = errors.New("Refund already done")
ErrInvalidTxnId = errors.New("Invalid Transaction Id")
ErrDuplicateReq = errors.New("Duplicate Request")
ErrSignatureMisMatch = errors.New("Signature MisMatch")
ErrRefundAgainstInvalidTxnType = errors.New("Refund not allowed against Txn with Non-Payment type")
ErrSaldoRefundAlreadyProcessed = errors.New("Saldo Redund Already processed")
ErrNoUnsettledBillFound = errors.New("No Unsettled Bill Found")
ErrInvalidOTP = errors.New("Invalid OTP")
ErrPreConditionFailedTest = errors.New("Precondition failed test")
ErrTokopediaEmail = errors.New("Tokopedia email id can not be used")
ErrNoBillFound = errors.New("No Bill found")
ErrBillDueDatePassed = errors.New("Last date to pay bill is over, pay your bill in next billing cycle.")
ErrNoOVOWallet = errors.New("OVO wallet not found")
ErrInvalidToken = errors.New("Invalid auth token")
ErrRequiredToken = errors.New("Auth token is required")
ErrUnauthorized = errors.New("No Authorization Found")
)
const (
STATUSCODE_GENERICSUCCESS = "200000"
STATUSCODE_BADREQUEST = "400000"
STATUSCODE_NOLINKEREXIST = "411553"
STATUSCODE_UNAUTHORIZED = "401000"
STATUS_FORBIDDEN = "403000"
STATUSCODE_NOT_FOUND = "404000"
STATUSCODE_GENERIC_PRECONDITION_FAILED = "412000" // todo error code change from 412 to 400 dur to nginx issue
STATUSCODE_INTERNAL_ERROR = "500000"
STATUSCODE_TIMEOUT_ERROR = "504000"
STATUSCODE_ALREADY_REGISTERED = "400001"
STATUSCODE_TX_ALREADY_DONE = "400003"
STATUSCODE_GENERIC_PRECONDITION_FAILED_TEST = "412000"
STATUSCODE_REQUIRED_TOKEN = "401000"
STATUS_DATA_EMPTY = "2000001"
)
func GetErrorCode(err error) int {
code, _ := strconv.ParseInt(GetErrorCodeStr(err), 10, 64)
return int(code)
}
func GetErrorCodeStr(err error) string {
switch err.(type) {
case custerr.ErrChain:
errType := err.(custerr.ErrChain).Type
if errType != nil {
err = errType
}
}
switch err {
case ErrBadRequest:
return STATUSCODE_BADREQUEST
case ErrForbiddenResource:
return STATUS_FORBIDDEN
case ErrNotFound:
return STATUSCODE_NOT_FOUND
case ErrPreConditionFailed:
return STATUSCODE_GENERIC_PRECONDITION_FAILED
case ErrInternalServerError:
return STATUSCODE_INTERNAL_ERROR
case ErrTimeoutError:
return STATUSCODE_TIMEOUT_ERROR
case ErrAlreadyRegistered:
return STATUSCODE_ALREADY_REGISTERED
case ErrDuplicateReq:
return STATUSCODE_ALREADY_REGISTERED
case ErrNoLinkerExists:
return STATUSCODE_NOLINKEREXIST
case nil:
return STATUSCODE_GENERICSUCCESS
case ErrNoValidUserFound:
return STATUSCODE_BADREQUEST
case ErrTxnAlreadyDone:
return STATUSCODE_TX_ALREADY_DONE
case ErrInvalidOTP:
return STATUSCODE_BADREQUEST
case ErrForbiddenNakama:
return STATUSCODE_BADREQUEST
case ErrPreConditionFailedTest:
return STATUSCODE_GENERIC_PRECONDITION_FAILED_TEST
case ErrTokopediaEmail:
return STATUSCODE_BADREQUEST
case ErrBillDueDatePassed:
return STATUSCODE_GENERIC_PRECONDITION_FAILED
case ErrNoBillFound:
return STATUSCODE_GENERIC_PRECONDITION_FAILED
case ErrNoOVOWallet:
return STATUSCODE_BADREQUEST
case ErrInvalidToken:
return STATUS_FORBIDDEN
case ErrRequiredToken:
return STATUSCODE_REQUIRED_TOKEN
case ErrUnauthorized:
return STATUSCODE_UNAUTHORIZED
default:
return STATUSCODE_INTERNAL_ERROR
}
}
func GetHTTPCode(code string) int {
s := code[0:3]
i, _ := strconv.Atoi(s)
return i
}
type JSONResponse struct {
Code string `json:"code"`
Message string `json:"message,omitempty"`
ErrorMessage []*err.ErrorFormat `json:"error_message,omitempty"`
ErrorString string `json:"error,omitempty"`
Data interface{} `json:"data,omitempty"`
Latency string `json:"latency"`
StatusCode int `json:"-"`
Error error `json:"-"`
Log map[string]interface{} `json:"-"`
Ctx context.Context `json:"-"`
Success *int `json:"success,omitempty"`
}
func NewJSONResponse() *JSONResponse {
return &JSONResponse{Ctx: context.Background(), Code: STATUSCODE_GENERICSUCCESS, StatusCode: GetHTTPCode(STATUSCODE_GENERICSUCCESS), Log: map[string]interface{}{}}
}
func NewJSONResponseWithCtx(ctx context.Context) *JSONResponse {
return &JSONResponse{Ctx: ctx, Code: STATUSCODE_GENERICSUCCESS, StatusCode: GetHTTPCode(STATUSCODE_GENERICSUCCESS), Log: map[string]interface{}{}}
}
func (r *JSONResponse) SetSuccess(code int) *JSONResponse {
r.Success = &code
return r
}
func (r *JSONResponse) SetData(data interface{}) *JSONResponse {
r.Data = data
return r
}
func (r *JSONResponse) SetMessage(msg string) *JSONResponse {
r.Message = msg
return r
}
func (r *JSONResponse) SetLatency(latency float64) *JSONResponse {
r.Latency = fmt.Sprintf("%.2f ms", latency)
return r
}
func (r *JSONResponse) SetLog(key string, val interface{}) *JSONResponse {
r.Log[key] = val
return r
}
func getErrType(err error) error {
switch err.(type) {
case custerr.ErrChain:
errType := err.(custerr.ErrChain).Type
if errType != nil {
err = errType
}
}
return err
}
func (r *JSONResponse) SetError(err error, a ...string) *JSONResponse {
err = getErrType(err)
r.Error = err
r.ErrorString = err.Error()
r.Code = GetErrorCodeStr(err)
r.StatusCode = GetHTTPCode(r.Code)
if r.StatusCode == http.StatusInternalServerError {
r.ErrorString = "Internal Server error"
}
if len(a) > 0 {
r.ErrorString = a[0]
}
return r
}
func (r *JSONResponse) Send(w http.ResponseWriter) {
b, _ := json.Marshal(r)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(r.StatusCode)
_, err := w.Write(b)
if err != nil {
log.Println(err)
}
}