Skip to content

Commit

Permalink
Server error handling when generating content (#207)
Browse files Browse the repository at this point in the history
Fixes #206

Check for server error HTTP status and immediately throw instead of
attempting to parse the body as JSON.
  • Loading branch information
Flucadetena authored Sep 4, 2024
1 parent 6082f2f commit 6a160fa
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
4 changes: 4 additions & 0 deletions pkgs/google_generative_ai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.6

- Throw a more relevant exception for server error HTTP responses.

## 0.4.5

- Add support for model side Code Execution. Enable code execution by
Expand Down
6 changes: 6 additions & 0 deletions pkgs/google_generative_ai/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'dart:convert';

import 'package:http/http.dart' as http;

import 'error.dart';
import 'version.dart';

const clientName = 'genai-dart/$packageVersion';
Expand Down Expand Up @@ -61,6 +62,11 @@ final class HttpApiClient implements ApiClient {
headers: await _headers(),
body: _utf8Json.encode(body),
);
if (response.statusCode >= 500) {
throw GenerativeAIException(
'Server Error [${response.statusCode}]: ${response.body}');
}

return _utf8Json.decode(response.bodyBytes) as Map<String, Object?>;
}

Expand Down
2 changes: 1 addition & 1 deletion pkgs/google_generative_ai/lib/src/version.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

const packageVersion = '0.4.5';
const packageVersion = '0.4.6';
2 changes: 1 addition & 1 deletion pkgs/google_generative_ai/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: google_generative_ai
# Update `lib/version.dart` when changing version.
version: 0.4.5
version: 0.4.6
description: >-
The Google AI Dart SDK enables developers to use Google's state-of-the-art
generative AI models (like Gemini).
Expand Down

0 comments on commit 6a160fa

Please sign in to comment.