mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
httpx.Error response without body (#982)
* httpx.Error support response without body * fix doc
This commit is contained in:
parent
423955c55f
commit
76fc1ef460
@ -90,7 +90,7 @@ After that, run these local verifications before submitting pull request to pred
|
||||
fail of continuous integration.
|
||||
|
||||
* Format the code with `gofmt`
|
||||
* Run the test with data race enabled `go test -race ./…`
|
||||
* Run the test with data race enabled `go test -race ./...`
|
||||
|
||||
## Code Review
|
||||
|
||||
|
@ -25,6 +25,11 @@ func Error(w http.ResponseWriter, err error) {
|
||||
}
|
||||
|
||||
code, body := errorHandler(err)
|
||||
if body == nil {
|
||||
w.WriteHeader(code)
|
||||
return
|
||||
}
|
||||
|
||||
e, ok := body.(error)
|
||||
if ok {
|
||||
http.Error(w, e.Error(), code)
|
||||
|
@ -28,12 +28,14 @@ func TestError(t *testing.T) {
|
||||
name string
|
||||
input string
|
||||
errorHandler func(error) (int, interface{})
|
||||
expectHaveBody bool
|
||||
expectBody string
|
||||
expectCode int
|
||||
}{
|
||||
{
|
||||
name: "default error handler",
|
||||
input: body,
|
||||
expectHaveBody: true,
|
||||
expectBody: body,
|
||||
expectCode: http.StatusBadRequest,
|
||||
},
|
||||
@ -43,6 +45,7 @@ func TestError(t *testing.T) {
|
||||
errorHandler: func(err error) (int, interface{}) {
|
||||
return http.StatusForbidden, err.Error()
|
||||
},
|
||||
expectHaveBody: true,
|
||||
expectBody: wrappedBody,
|
||||
expectCode: http.StatusForbidden,
|
||||
},
|
||||
@ -52,9 +55,20 @@ func TestError(t *testing.T) {
|
||||
errorHandler: func(err error) (int, interface{}) {
|
||||
return http.StatusForbidden, err
|
||||
},
|
||||
expectHaveBody: true,
|
||||
expectBody: body,
|
||||
expectCode: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "customized error handler return nil",
|
||||
input: body,
|
||||
errorHandler: func(err error) (int, interface{}) {
|
||||
return http.StatusForbidden, nil
|
||||
},
|
||||
expectHaveBody: false,
|
||||
expectBody: "",
|
||||
expectCode: http.StatusForbidden,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@ -75,6 +89,7 @@ func TestError(t *testing.T) {
|
||||
}
|
||||
Error(&w, errors.New(test.input))
|
||||
assert.Equal(t, test.expectCode, w.code)
|
||||
assert.Equal(t, test.expectHaveBody, w.haveBody)
|
||||
assert.Equal(t, test.expectBody, strings.TrimSpace(w.builder.String()))
|
||||
})
|
||||
}
|
||||
@ -122,6 +137,7 @@ func TestWriteJsonLessWritten(t *testing.T) {
|
||||
type tracedResponseWriter struct {
|
||||
headers map[string][]string
|
||||
builder strings.Builder
|
||||
haveBody bool
|
||||
code int
|
||||
lessWritten bool
|
||||
timeout bool
|
||||
@ -140,6 +156,7 @@ func (w *tracedResponseWriter) Write(bytes []byte) (n int, err error) {
|
||||
if w.lessWritten {
|
||||
n -= 1
|
||||
}
|
||||
w.haveBody = true
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user