Browse Source

fix the panic of getting out of index with response header

tags/v0.7.8
toranger 6 years ago
parent
commit
0e9d0ccc17
  1. 4
      error.go
  2. 33
      error_test.go

4
error.go

@ -24,11 +24,11 @@ type ErrorResponse struct {
func (r *ErrorResponse) Error() string {
RequestID := r.RequestID
if RequestID == "" {
RequestID = r.Response.Header["X-Cos-Request-Id"][0]
RequestID = r.Response.Header.Get("X-Cos-Request-Id")
}
TraceID := r.TraceID
if TraceID == "" {
TraceID = r.Response.Header["X-Cos-Trace-Id"][0]
TraceID = r.Response.Header.Get("X-Cos-Trace-Id")
}
return fmt.Sprintf("%v %v: %d %v(Message: %v, RequestId: %v, TraceId: %v)",
r.Response.Request.Method, r.Response.Request.URL,

33
error_test.go

@ -54,3 +54,36 @@ func Test_checkResponse_no_error(t *testing.T) {
t.Errorf("Expected error == nil, got %+v", err)
}
}
func Test_checkResponse_with_error(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/test_409", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusConflict)
fmt.Fprint(w, `<?xml version='1.0' encoding='utf-8' ?>
<Error>
<Code>BucketAlreadyExists</Code>
<Message>The requested bucket name is not available.</Message>
<Resource>testdelete-1253846586.cos.ap-guangzhou.myqcloud.com</Resource>
<RequestId>NTk0NTRjZjZfNTViMjM1XzlkMV9hZTZh</RequestId>
<TraceId>OGVmYzZiMmQzYjA2OWNhODk0NTRkMTBiOWVmMDAxODc0OWRkZjk0ZDM1NmI1M2E2MTRlY2MzZDhmNmI5MWI1OTBjYzE2MjAxN2M1MzJiOTdkZjMxMDVlYTZjN2FiMmI0NTk3NWFiNjAyMzdlM2RlMmVmOGNiNWIxYjYwNDFhYmQ=</TraceId>
</Error>`)
})
req, _ := http.NewRequest("GET", client.BaseURL.ServiceURL.String()+"/test_409", nil)
resp, _ := client.client.Do(req)
err := checkResponse(resp)
if e, ok := err.(*ErrorResponse); ok {
if e.Error() == "" {
t.Errorf("Expected e.Error() not empty, got %+v", e.Error())
}
if e.Code != "BucketAlreadyExists" {
t.Errorf("Expected BucketAlreadyExists error, got %+v", e.Code)
}
} else {
t.Errorf("Expected ErrorResponse error, got %+v", err)
}
}
Loading…
Cancel
Save