Skip to content

Commit

Permalink
fix(client): Throw a ClientException on non-json responses, to be
Browse files Browse the repository at this point in the history
 consistent with other malformed responses.
 Fix tests to also expect a ClientException in theses cases.
 Fixes #552.
  • Loading branch information
Martin Kamleithner committed Jan 31, 2020
1 parent 39c640f commit 7d538e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 6 additions & 2 deletions packages/graphql/lib/src/link/http/link_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,12 @@ Future<FetchResult> _parseResponse(StreamedResponse response) async {
final Uint8List responseByte = await response.stream.toBytes();
final String decodedBody = encoding.decode(responseByte);

final Map<String, dynamic> jsonResponse =
json.decode(decodedBody) as Map<String, dynamic>;
Map<String, dynamic> jsonResponse;
try {
jsonResponse= json.decode(decodedBody) as Map<String, dynamic>;
}catch(e){
throw ClientException('Invalid response body: $decodedBody');
}
final FetchResult fetchResult = FetchResult();

if (jsonResponse['errors'] != null) {
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql/test/link/http/link_http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,12 @@ void main() {

expect(
exception,
const TypeMatcher<UnhandledFailureWrapper>(),
const TypeMatcher<ClientException>(),
);

expect(
(exception as UnhandledFailureWrapper).failure,
const TypeMatcher<FormatException>(),
(exception as ClientException).message,
"Invalid response body: ",
);
});

Expand Down

0 comments on commit 7d538e1

Please sign in to comment.