Optimize Client Error Return (#856)
* update client error return * update client_test.go * update client_test.go * update file_api_test.go * update client_test.go * update client_test.go
This commit is contained in:
@@ -134,14 +134,17 @@ func TestHandleErrorResp(t *testing.T) {
|
||||
client := NewClient(mockToken)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
httpCode int
|
||||
body io.Reader
|
||||
expected string
|
||||
name string
|
||||
httpCode int
|
||||
httpStatus string
|
||||
contentType string
|
||||
body io.Reader
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "401 Invalid Authentication",
|
||||
httpCode: http.StatusUnauthorized,
|
||||
name: "401 Invalid Authentication",
|
||||
httpCode: http.StatusUnauthorized,
|
||||
contentType: "application/json",
|
||||
body: bytes.NewReader([]byte(
|
||||
`{
|
||||
"error":{
|
||||
@@ -152,11 +155,12 @@ func TestHandleErrorResp(t *testing.T) {
|
||||
}
|
||||
}`,
|
||||
)),
|
||||
expected: "error, status code: 401, message: You didn't provide an API key. ....",
|
||||
expected: "error, status code: 401, status: , message: You didn't provide an API key. ....",
|
||||
},
|
||||
{
|
||||
name: "401 Azure Access Denied",
|
||||
httpCode: http.StatusUnauthorized,
|
||||
name: "401 Azure Access Denied",
|
||||
httpCode: http.StatusUnauthorized,
|
||||
contentType: "application/json",
|
||||
body: bytes.NewReader([]byte(
|
||||
`{
|
||||
"error":{
|
||||
@@ -165,11 +169,12 @@ func TestHandleErrorResp(t *testing.T) {
|
||||
}
|
||||
}`,
|
||||
)),
|
||||
expected: "error, status code: 401, message: Access denied due to Virtual Network/Firewall rules.",
|
||||
expected: "error, status code: 401, status: , message: Access denied due to Virtual Network/Firewall rules.",
|
||||
},
|
||||
{
|
||||
name: "503 Model Overloaded",
|
||||
httpCode: http.StatusServiceUnavailable,
|
||||
name: "503 Model Overloaded",
|
||||
httpCode: http.StatusServiceUnavailable,
|
||||
contentType: "application/json",
|
||||
body: bytes.NewReader([]byte(`
|
||||
{
|
||||
"error":{
|
||||
@@ -179,22 +184,53 @@ func TestHandleErrorResp(t *testing.T) {
|
||||
"code":null
|
||||
}
|
||||
}`)),
|
||||
expected: "error, status code: 503, message: That model...",
|
||||
expected: "error, status code: 503, status: , message: That model...",
|
||||
},
|
||||
{
|
||||
name: "503 no message (Unknown response)",
|
||||
httpCode: http.StatusServiceUnavailable,
|
||||
name: "503 no message (Unknown response)",
|
||||
httpCode: http.StatusServiceUnavailable,
|
||||
contentType: "application/json",
|
||||
body: bytes.NewReader([]byte(`
|
||||
{
|
||||
"error":{}
|
||||
}`)),
|
||||
expected: "error, status code: 503, message: ",
|
||||
expected: "error, status code: 503, status: , message: ",
|
||||
},
|
||||
{
|
||||
name: "413 Request Entity Too Large",
|
||||
httpCode: http.StatusRequestEntityTooLarge,
|
||||
contentType: "text/html",
|
||||
body: bytes.NewReader([]byte(`<html>
|
||||
<head><title>413 Request Entity Too Large</title></head>
|
||||
<body>
|
||||
<center><h1>413 Request Entity Too Large</h1></center>
|
||||
<hr><center>nginx</center>
|
||||
</body>
|
||||
</html>`)),
|
||||
expected: `error, status code: 413, status: , body: <html>
|
||||
<head><title>413 Request Entity Too Large</title></head>
|
||||
<body>
|
||||
<center><h1>413 Request Entity Too Large</h1></center>
|
||||
<hr><center>nginx</center>
|
||||
</body>
|
||||
</html>`,
|
||||
},
|
||||
{
|
||||
name: "errorReader",
|
||||
httpCode: http.StatusRequestEntityTooLarge,
|
||||
contentType: "text/html",
|
||||
body: &errorReader{err: errors.New("errorReader")},
|
||||
expected: "error, reading response body: errorReader",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
testCase := &http.Response{}
|
||||
testCase := &http.Response{
|
||||
Header: map[string][]string{
|
||||
"Content-Type": {tc.contentType},
|
||||
},
|
||||
}
|
||||
testCase.StatusCode = tc.httpCode
|
||||
testCase.Body = io.NopCloser(tc.body)
|
||||
err := client.handleErrorResp(testCase)
|
||||
@@ -203,12 +239,6 @@ func TestHandleErrorResp(t *testing.T) {
|
||||
t.Errorf("Unexpected error: %v , expected: %s", err, tc.expected)
|
||||
t.Fail()
|
||||
}
|
||||
|
||||
e := &APIError{}
|
||||
if !errors.As(err, &e) {
|
||||
t.Errorf("(%s) Expected error to be of type APIError", tc.name)
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user