Skip to content

Commit 92c2e8a

Browse files
authored
api: fix unreachable status err (ollama#11423)
StatusError was unreachable, the client always checked for error messages in the response body first, and the server always includes error messages with HTTP error status codes.
1 parent 2e3fd86 commit 92c2e8a

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

api/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,6 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f
222222
return fmt.Errorf("unmarshal: %w", err)
223223
}
224224

225-
if errorResponse.Error != "" {
226-
return errors.New(errorResponse.Error)
227-
}
228-
229225
if response.StatusCode >= http.StatusBadRequest {
230226
return StatusError{
231227
StatusCode: response.StatusCode,
@@ -234,6 +230,10 @@ func (c *Client) stream(ctx context.Context, method, path string, data any, fn f
234230
}
235231
}
236232

233+
if errorResponse.Error != "" {
234+
return errors.New(errorResponse.Error)
235+
}
236+
237237
if err := fn(bts); err != nil {
238238
return err
239239
}

api/client_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ func TestClientStream(t *testing.T) {
8989
},
9090
wantErr: "mid-stream error",
9191
},
92+
{
93+
name: "http status error takes precedence over general error",
94+
responses: []any{
95+
testError{
96+
message: "custom error message",
97+
statusCode: http.StatusInternalServerError,
98+
},
99+
},
100+
wantErr: "500",
101+
},
92102
{
93103
name: "successful stream completion",
94104
responses: []any{

0 commit comments

Comments
 (0)