Skip to content

Commit fafcfff

Browse files
truongsinhmicimize
authored andcommitted
fix bug of empty error (#5)
1 parent 295512d commit fafcfff

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

lib/src/link/http/link_http.dart

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -281,37 +281,34 @@ HttpHeadersAndBody _selectHttpOptionsAndBody(
281281

282282
Future<FetchResult> _parseResponse(StreamedResponse response) async {
283283
final int statusCode = response.statusCode;
284-
final String reasonPhrase = response.reasonPhrase;
285284

286-
try {
287-
final Encoding encoding = _determineEncodingFromResponse(response);
288-
// @todo limit bodyBytes
289-
final Uint8List reponseByte = await response.stream.toBytes();
290-
final String decodedBody = encoding.decode(reponseByte);
285+
final Encoding encoding = _determineEncodingFromResponse(response);
286+
// @todo limit bodyBytes
287+
final Uint8List responseByte = await response.stream.toBytes();
288+
final String decodedBody = encoding.decode(responseByte);
291289

292-
final Map<String, dynamic> jsonResponse =
293-
json.decode(decodedBody) as Map<String, dynamic>;
294-
final FetchResult fetchResult = FetchResult();
290+
final Map<String, dynamic> jsonResponse =
291+
json.decode(decodedBody) as Map<String, dynamic>;
292+
final FetchResult fetchResult = FetchResult();
295293

296-
if (jsonResponse['errors'] != null) {
297-
fetchResult.errors = jsonResponse['errors'] as List<dynamic>;
298-
}
294+
if (jsonResponse['errors'] != null) {
295+
fetchResult.errors = jsonResponse['errors'] as List<dynamic>;
296+
}
299297

300-
if (jsonResponse['data'] != null) {
301-
fetchResult.data = jsonResponse['data'];
302-
}
298+
if (jsonResponse['data'] != null) {
299+
fetchResult.data = jsonResponse['data'];
300+
}
303301

304-
return fetchResult;
305-
} catch (e) {
306-
// @todo get body as well;
307-
// final String decodedBody = encoding.decode(response.bodyBytes);
302+
if (fetchResult.data == null && fetchResult.errors == null) {
308303
if (statusCode < 200 || statusCode >= 400) {
309304
throw ClientException(
310-
'Network Error: $statusCode $reasonPhrase',
305+
'Network Error: $statusCode $decodedBody',
311306
);
312307
}
313-
rethrow;
308+
throw ClientException('Invalid response body: $decodedBody');
314309
}
310+
311+
return fetchResult;
315312
}
316313

317314
/// Returns the charset encoding for the given response.

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version: 1.0.0-beta.1+1
44
authors:
55
- Eus Dima <eus@zinoapp.com>
66
- Zino Hofmann <zino@zinoapp.com>
7+
- TruongSinh Tran-Nguyen <i@truongsinh.pro>
78
homepage: https://github.com/zino-app/graphql-flutter/tree/next
89

910
dependencies:

test/graphql_flutter_test.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ void main() {
142142
});
143143
// test('failed query because of network', {});
144144
// test('failed query because of because of error response', {});
145-
// test('failed query because of because of invalid response', {});
145+
// test('failed query because of because of invalid response', () {
146+
// String responseBody =
147+
// '{\"message\":\"Bad credentials\",\"documentation_url\":\"https://developer.github.com/v4\"}';
148+
// int responseCode = 401;
149+
// });
150+
// test('partially success query with some errors', {});
146151
});
147152
group('mutation', () {
148153
test('successful mutation', () async {
@@ -357,5 +362,11 @@ void main() {
357362
},
358363
]);
359364
});
365+
366+
// test('upload fail error response', () {
367+
// const String responseBody =
368+
// r'{"errors":[{"message":"Variable \"$files\" of required type \"[Upload!]!\" was not provided.","locations":[{"line":1,"column":14}],"extensions":{"code":"INTERNAL_SERVER_ERROR","exception":{"stacktrace":["GraphQLError: Variable \"$files\" of required type \"[Upload!]!\" was not provided."," at getVariableValues (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/graphql/execution/values.js:76:21)"," at buildExecutionContext (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/graphql/execution/execute.js:196:63)"," at executeImpl (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/graphql/execution/execute.js:70:20)"," at Object.execute (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/graphql/execution/execute.js:62:35)"," at /Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/apollo-server-core/dist/requestPipeline.js:195:36"," at Generator.next (<anonymous>)"," at /Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/apollo-server-core/dist/requestPipeline.js:7:71"," at new Promise (<anonymous>)"," at __awaiter (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/apollo-server-core/dist/requestPipeline.js:3:12)"," at execute (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/apollo-server-core/dist/requestPipeline.js:179:20)"," at Object.<anonymous> (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/apollo-server-core/dist/requestPipeline.js:131:35)"," at Generator.next (<anonymous>)"," at fulfilled (/Users/truongsinh/Dev/flutter/graphql-flutter/example/server/api/node_modules/apollo-server-core/dist/requestPipeline.js:4:58)"," at process._tickCallback (internal/process/next_tick.js:68:7)"]}}}]';
369+
// const int statusCode = 400;
370+
// });
360371
});
361372
}

0 commit comments

Comments
 (0)