Skip to content

Commit e3f0693

Browse files
committed
回滚
1 parent 38df488 commit e3f0693

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

http/middleware/recovery/recovery_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Test_Recovery(t *testing.T) {
3030
should.NoError(err)
3131

3232
router.ServeHTTP(recorder, req)
33-
should.Equal(recorder.Code, http.StatusOK)
33+
should.Equal(recorder.Code, http.StatusInternalServerError)
3434
}
3535

3636
func init() {

http/response/send.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ func Failed(w http.ResponseWriter, err error) {
2626
errCode = exception.UnKnownException
2727
}
2828

29-
// 统一使用业务code, http code固定200
30-
httpCode = http.StatusOK
29+
// 映射http status code 1xx - 5xx
30+
// 如果为其他errCode, 统一成200
31+
if errCode/100 >= 1 && errCode/100 <= 5 {
32+
httpCode = errCode
33+
} else {
34+
httpCode = http.StatusOK
35+
}
3136

3237
resp := Data{
3338
Code: &errCode,
@@ -38,7 +43,6 @@ func Failed(w http.ResponseWriter, err error) {
3843

3944
// set response heanders
4045
w.Header().Set("Content-Type", "application/json")
41-
w.Header().Set("Err-Code", fmt.Sprintf("%d", errCode))
4246

4347
// if marshal json error, use string to response
4448
respByt, err := json.Marshal(resp)

http/router/httprouter/httprouter_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestWithAutherFailed(t *testing.T) {
115115
r.Handle("GET", "/", IndexHandler).EnableAuth()
116116
r.ServeHTTP(w, req)
117117

118-
should.Equal(200, w.Code)
118+
should.Equal(401, w.Code)
119119
}
120120

121121
func TestWithAutherOK(t *testing.T) {
@@ -160,7 +160,7 @@ func TestWithParamsFailed(t *testing.T) {
160160
r.Handle("GET", "/:id", WithContextHandler)
161161
r.ServeHTTP(w, req)
162162

163-
should.Equal(200, w.Code)
163+
should.Equal(400, w.Code)
164164
}
165165

166166
func TestSetLabel(t *testing.T) {

0 commit comments

Comments
 (0)