Skip to content

Commit

Permalink
Improved request error handling in generated client
Browse files Browse the repository at this point in the history
  • Loading branch information
walsha2 committed Nov 14, 2023
1 parent 0a70706 commit 332120f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 47 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Changelog

## 0.7.7

* Improved request error handling in generated client

## 0.7.6

* Ensure that safe enum logic is propogated for all enum generation code
* Ensure that safe enum logic is propagated for all enum generation code

## 0.7.5

Expand Down
132 changes: 87 additions & 45 deletions lib/src/generators/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ class $clientName {
}
// ------------------------------------------
// METHOD: makeRequestStream
// METHOD: _request
// ------------------------------------------
/// Reusable request stream method
/// Reusable request method
@protected
Future<http.StreamedResponse> makeRequestStream({
Future<http.StreamedResponse> _request({
required String baseUrl,
required String path,
required HttpMethod method,
Expand Down Expand Up @@ -318,46 +318,75 @@ class $clientName {
headers.addAll(this.headers);
// Build the request object
late http.StreamedResponse response;
try {
http.BaseRequest request;
if (isMultipart) {
// Handle multipart request
request = http.MultipartRequest(method.name, uri);
request = request as http.MultipartRequest;
if (body is List<http.MultipartFile>) {
request.files.addAll(body);
} else {
request.files.add(body as http.MultipartFile);
}
http.BaseRequest request;
if (isMultipart) {
// Handle multipart request
request = http.MultipartRequest(method.name, uri);
request = request as http.MultipartRequest;
if (body is List<http.MultipartFile>) {
request.files.addAll(body);
} else {
// Handle normal request
request = http.Request(method.name, uri);
request = request as http.Request;
try {
if (body != null) {
request.body = json.encode(body);
}
} catch (e) {
// Handle request encoding error
throw $clientException(
uri: uri,
method: method,
message: 'Could not encode: \${body.runtimeType}',
body: e,
);
request.files.add(body as http.MultipartFile);
}
} else {
// Handle normal request
request = http.Request(method.name, uri);
request = request as http.Request;
try {
if (body != null) {
request.body = json.encode(body);
}
} catch (e) {
// Handle request encoding error
throw $clientException(
uri: uri,
method: method,
message: 'Could not encode: \${body.runtimeType}',
body: e,
);
}
}
// Add request headers
request.headers.addAll(headers);
// Add request headers
request.headers.addAll(headers);
// Handle user request middleware
request = await onRequest(request);
// Handle user request middleware
request = await onRequest(request);
// Submit request
response = await client.send(request);
// Submit request
return await client.send(request);
}
// ------------------------------------------
// METHOD: makeRequestStream
// ------------------------------------------
/// Reusable request stream method
@protected
Future<http.StreamedResponse> makeRequestStream({
required String baseUrl,
required String path,
required HttpMethod method,
Map<String, dynamic> queryParams = const {},
Map<String, String> headerParams = const {},
bool isMultipart = false,
String requestType = '',
String responseType = '',
Object? body,
}) async {
final uri = Uri.parse((this.baseUrl ?? baseUrl) + path);
late http.StreamedResponse response;
try {
response = await _request(
baseUrl: baseUrl,
path: path,
method: method,
queryParams: queryParams,
headerParams: headerParams,
requestType: requestType,
responseType: responseType,
body: body,
);
// Handle user response middleware
response = await onStreamedResponse(response);
} catch (e) {
Expand All @@ -384,11 +413,11 @@ class $clientName {
body: (await http.Response.fromStream(response)).body,
);
}
// ------------------------------------------
// METHOD: makeRequest
// ------------------------------------------
/// Reusable request method
@protected
Future<http.Response> makeRequest({
Expand All @@ -402,8 +431,10 @@ class $clientName {
String responseType = '',
Object? body,
}) async {
final uri = Uri.parse((this.baseUrl ?? baseUrl) + path);
late http.Response response;
try {
final streamedResponse = await makeRequestStream(
final streamedResponse = await _request(
baseUrl: baseUrl,
path: path,
method: method,
Expand All @@ -413,21 +444,32 @@ class $clientName {
responseType: responseType,
body: body,
);
final response = await http.Response.fromStream(streamedResponse);
response = await http.Response.fromStream(streamedResponse);
// Handle user response middleware
return await onResponse(response);
} on $clientException {
rethrow;
response = await onResponse(response);
} catch (e) {
// Handle request and response errors
throw $clientException(
uri: Uri.parse((this.baseUrl ?? baseUrl) + path),
uri: uri,
method: method,
message: 'Response error',
body: e,
);
}
// Check for successful response
if ((response.statusCode ~/ 100) == 2) {
return response;
}
// Handle unsuccessful response
throw $clientException(
uri: uri,
method: method,
message: 'Unsuccessful response',
code: response.statusCode,
body: response.body,
);
}\n
""");

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: openapi_spec
description: OpenAPI Specification generator using native Dart code, as well as an all-in-one parser of existing specifications.
version: 0.7.6
version: 0.7.7
maintainer: Taza Technology LLC
repository: https://github.com/tazatechnology/openapi_spec
issue_tracker: https://github.com/tazatechnology/openapi_spec/issues
Expand Down

0 comments on commit 332120f

Please sign in to comment.