Skip to content

Commit 12adbfa

Browse files
qiaodevcopybara-github
authored andcommitted
feat!: rename ClientError and ServerError to APIError. fixes: #159
PiperOrigin-RevId: 740426404
1 parent 5251bf6 commit 12adbfa

File tree

2 files changed

+23
-50
lines changed

2 files changed

+23
-50
lines changed

api_client.go

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,20 @@ func iterateResponseStream[R any](rs *responseStream[R], responseConverter func(
223223
}
224224
}
225225

226-
type apiError struct {
227-
Code int `json:"code,omitempty"`
228-
Message string `json:"message,omitempty"`
229-
Status string `json:"status,omitempty"`
226+
// APIError contains an error response from the server.
227+
type APIError struct {
228+
// Code is the HTTP response status code.
229+
Code int `json:"code,omitempty"`
230+
// Message is the server response message.
231+
Message string `json:"message,omitempty"`
232+
// Status is the server response status.
233+
Status string `json:"status,omitempty"`
234+
// Details provide more context to an error.
230235
Details []map[string]any `json:"details,omitempty"`
231236
}
232237

233238
type responseWithError struct {
234-
ErrorInfo *apiError `json:"error,omitempty"`
239+
ErrorInfo *APIError `json:"error,omitempty"`
235240
}
236241

237242
func newAPIError(resp *http.Response) error {
@@ -245,41 +250,15 @@ func newAPIError(resp *http.Response) error {
245250
if err := json.Unmarshal(body, respWithError); err != nil {
246251
return fmt.Errorf("newAPIError: unmarshal response to error failed: %w. Response: %v", err, string(body))
247252
}
248-
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
249-
return ClientError{apiError: *respWithError.ErrorInfo}
250-
}
251-
return ServerError{apiError: *respWithError.ErrorInfo}
252-
}
253-
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
254-
return ClientError{apiError: apiError{Code: resp.StatusCode, Status: resp.Status}}
253+
return *respWithError.ErrorInfo
255254
}
256-
return ServerError{apiError: apiError{Code: resp.StatusCode, Status: resp.Status}}
257-
}
258-
259-
// ClientError is an error that occurs when the GenAI API
260-
// receives an invalid request from a client.
261-
type ClientError struct {
262-
apiError
263-
}
264-
265-
// Error returns a string representation of the ClientError.
266-
func (e ClientError) Error() string {
267-
return fmt.Sprintf(
268-
"client error. Code: %d, Message: %s, Status: %s, Details: %v",
269-
e.Code, e.Message, e.Status, e.Details,
270-
)
271-
}
272-
273-
// ServerError is an error that occurs when the GenAI API
274-
// encounters an unexpected server problem.
275-
type ServerError struct {
276-
apiError
255+
return APIError{Code: resp.StatusCode, Status: resp.Status}
277256
}
278257

279258
// Error returns a string representation of the ServerError.
280-
func (e ServerError) Error() string {
259+
func (e APIError) Error() string {
281260
return fmt.Sprintf(
282-
"server error. Code: %d, Message: %s, Status: %s, Details: %v",
261+
"Error %d, Message: %s, Status: %s, Details: %v",
283262
e.Code, e.Message, e.Status, e.Details,
284263
)
285264
}

api_client_test.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ func TestSendRequest(t *testing.T) {
7979
method: http.MethodGet,
8080
responseCode: http.StatusBadRequest,
8181
responseBody: `{"error": {"code": 400, "message": "bad request", "status": "INVALID_ARGUMENTS", "details": [{"field": "value"}]}}`,
82-
wantErr: &ClientError{apiError: apiError{Code: http.StatusBadRequest, Message: ""}},
82+
wantErr: &APIError{Code: http.StatusBadRequest, Message: "bad request", Details: []map[string]any{{"field": "value"}}},
8383
},
8484
{
8585
desc: "500 error response",
8686
path: "bar",
8787
method: http.MethodGet,
8888
responseCode: http.StatusInternalServerError,
8989
responseBody: `{"error": {"code": 500, "message": "internal server error", "status": "INTERNAL_SERVER_ERROR", "details": [{"field": "value"}]}}`,
90-
wantErr: &ServerError{apiError: apiError{Code: http.StatusInternalServerError, Message: ""}},
90+
wantErr: &APIError{Code: http.StatusInternalServerError, Message: "internal server error", Details: []map[string]any{{"field": "value"}}},
9191
},
9292
{
9393
desc: "invalid response body",
@@ -126,16 +126,10 @@ func TestSendRequest(t *testing.T) {
126126
}
127127
if tt.wantErr != nil && err != nil {
128128
// For error cases, check for want error types
129-
if tt.responseCode >= 400 && tt.responseCode < 500 {
130-
_, ok := err.(ClientError)
129+
if tt.responseCode >= 400 {
130+
_, ok := err.(APIError)
131131
if !ok {
132-
t.Errorf("want ClientError, got %T(%s)", err, err.Error())
133-
}
134-
135-
} else if tt.responseCode >= 500 {
136-
_, ok := err.(ServerError)
137-
if !ok {
138-
t.Errorf("want ServerError, got %T", err)
132+
t.Errorf("want Error, got %T(%s)", err, err.Error())
139133
}
140134
} else if tt.path == "" { // build request error
141135
if !strings.Contains(err.Error(), tt.wantErr.Error()) {
@@ -303,7 +297,7 @@ func TestSendStreamRequest(t *testing.T) {
303297
mockResponse: `{"error":{"code":400,"message":"test error message","status":"INVALID_ARGUMENT"}}`,
304298
mockStatusCode: http.StatusBadRequest,
305299
wantErr: true,
306-
wantErrorMessage: "client error. Code: 400, Message: test error message, Status: INVALID_ARGUMENT, Details: []",
300+
wantErrorMessage: "Error 400, Message: test error message, Status: INVALID_ARGUMENT, Details: []",
307301
},
308302
{
309303
name: "Error Response with empty body",
@@ -313,7 +307,7 @@ func TestSendStreamRequest(t *testing.T) {
313307
mockResponse: ``,
314308
mockStatusCode: http.StatusBadRequest,
315309
wantErr: true,
316-
wantErrorMessage: "client error. Code: 400, Message: , Status: 400 Bad Request, Details: []",
310+
wantErrorMessage: "Error 400, Message: , Status: 400 Bad Request, Details: []",
317311
},
318312
{
319313
name: "Error Response with invalid json",
@@ -333,7 +327,7 @@ func TestSendStreamRequest(t *testing.T) {
333327
mockResponse: `{"error":{"code":500,"message":"test error message","status":"INTERNAL"}}`,
334328
mockStatusCode: http.StatusInternalServerError,
335329
wantErr: true,
336-
wantErrorMessage: "server error. Code: 500, Message: test error message, Status: INTERNAL, Details: []",
330+
wantErrorMessage: "Error 500, Message: test error message, Status: INTERNAL, Details: []",
337331
},
338332
{
339333
name: "Error Response with server error and empty body",
@@ -343,7 +337,7 @@ func TestSendStreamRequest(t *testing.T) {
343337
mockResponse: ``,
344338
mockStatusCode: http.StatusInternalServerError,
345339
wantErr: true,
346-
wantErrorMessage: "server error. Code: 500, Message: , Status: 500 Internal Server Error, Details: []",
340+
wantErrorMessage: "Error 500, Message: , Status: 500 Internal Server Error, Details: []",
347341
},
348342
{
349343
name: "Error Response with server error and invalid json",

0 commit comments

Comments
 (0)