From 8790207ff367e77e8920495c91dae2a3b9b30d4b Mon Sep 17 00:00:00 2001 From: Job Guldemeester Date: Thu, 4 Jan 2024 11:54:14 +0100 Subject: [PATCH] :art: Updated exception based on feedback. Added test for void. --- chopper/lib/src/chopper_http_exception.dart | 13 +++++++++ chopper/lib/src/response.dart | 3 ++- chopper/test/response_test.dart | 27 ++++++++++++------- ...test_without_response_service.chopper.dart | 4 +-- .../test/test_without_response_service.dart | 2 +- ...test_without_response_service.chopper.dart | 4 +-- .../test/test_without_response_service.dart | 2 +- 7 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 chopper/lib/src/chopper_http_exception.dart diff --git a/chopper/lib/src/chopper_http_exception.dart b/chopper/lib/src/chopper_http_exception.dart new file mode 100644 index 00000000..cae57ce2 --- /dev/null +++ b/chopper/lib/src/chopper_http_exception.dart @@ -0,0 +1,13 @@ +import 'package:chopper/src/response.dart'; + +/// An exception thrown when a [Response] is unsuccessful < 200 or > 300. +class ChopperHttpException implements Exception { + ChopperHttpException(this.response); + + final Response response; + + @override + String toString() { + return 'Could not fetch the response for ${response.base.request}. Status code: ${response.statusCode}, error: ${response.error}'; + } +} diff --git a/chopper/lib/src/response.dart b/chopper/lib/src/response.dart index 22e07c27..68d1d340 100644 --- a/chopper/lib/src/response.dart +++ b/chopper/lib/src/response.dart @@ -1,6 +1,7 @@ import 'dart:io'; import 'dart:typed_data'; +import 'package:chopper/src/chopper_http_exception.dart'; import 'package:equatable/equatable.dart' show EquatableMixin; import 'package:http/http.dart' as http; import 'package:meta/meta.dart'; @@ -87,7 +88,7 @@ base class Response with EquatableMixin { if (error is Exception) { throw error!; } - throw HttpException('Could not fetch response $statusCode: $error'); + throw ChopperHttpException(this); } } diff --git a/chopper/test/response_test.dart b/chopper/test/response_test.dart index b612cef1..34167a95 100644 --- a/chopper/test/response_test.dart +++ b/chopper/test/response_test.dart @@ -1,8 +1,7 @@ -import 'dart:io'; - +import 'package:chopper/src/chopper_http_exception.dart'; import 'package:chopper/src/response.dart'; -import 'package:test/test.dart'; import 'package:http/http.dart' as http; +import 'package:test/test.dart'; import 'fixtures/error_fixtures.dart'; @@ -81,25 +80,35 @@ void main() { final base = http.Response('Foobar', 400); final response = Response(base, '', error: 'Error occurred'); - expect(() => response.bodyOrThrow, throwsA(isA())); + expect(() => response.bodyOrThrow, throwsA(isA())); }); test( - 'Response is unsuccessful and has no error, [bodyOrThrow throws HttpException]', + 'Response is unsuccessful and has no error, [bodyOrThrow throws ChopperHttpException]', () { final base = http.Response('Foobar', 400); final response = Response(base, ''); - expect(() => response.bodyOrThrow, throwsA(isA())); + expect(() => response.bodyOrThrow, throwsA(isA())); }); test( - 'Response is successful and has no body, [bodyOrThrow throws HttpException]', + 'Response is successful and has no body, [bodyOrThrow throws ChopperHttpException]', + () { + final base = http.Response('Foobar', 200); + final Response response = Response(base, null); + + expect(() => response.bodyOrThrow, throwsA(isA())); + }); + + test('Response is successful and has void body, [bodyOrThrow returns void]', () { final base = http.Response('Foobar', 200); - final response = Response(base, null); + // Ignoring void checks for testing purposes + //ignore: void_checks + final Response response = Response(base, Null); - expect(() => response.bodyOrThrow, throwsA(isA())); + expect(() => response.bodyOrThrow, returnsNormally); }); }); } diff --git a/chopper/test/test_without_response_service.chopper.dart b/chopper/test/test_without_response_service.chopper.dart index 5501d0b5..c7b8462a 100644 --- a/chopper/test/test_without_response_service.chopper.dart +++ b/chopper/test/test_without_response_service.chopper.dart @@ -269,7 +269,7 @@ final class _$HttpTestService extends HttpTestService { } @override - Future deleteTest(String id) async { + Future deleteTest(String id) async { final Uri $url = Uri.parse('/test/delete/${id}'); final Map $headers = { 'foo': 'bar', @@ -280,7 +280,7 @@ final class _$HttpTestService extends HttpTestService { client.baseUrl, headers: $headers, ); - final Response $response = await client.send($request); + final Response $response = await client.send($request); return $response.bodyOrThrow; } diff --git a/chopper/test/test_without_response_service.dart b/chopper/test/test_without_response_service.dart index 1a0ac821..d8a95417 100644 --- a/chopper/test/test_without_response_service.dart +++ b/chopper/test/test_without_response_service.dart @@ -80,7 +80,7 @@ abstract class HttpTestService extends ChopperService { Future putTest(@Path('id') String test, @Body() String data); @Delete(path: 'delete/{id}', headers: {'foo': 'bar'}) - Future deleteTest(@Path() String id); + Future deleteTest(@Path() String id); @Patch(path: 'patch/{id}') Future patchTest(@Path() String id, @Body() String data); diff --git a/chopper_generator/test/test_without_response_service.chopper.dart b/chopper_generator/test/test_without_response_service.chopper.dart index 5501d0b5..c7b8462a 100644 --- a/chopper_generator/test/test_without_response_service.chopper.dart +++ b/chopper_generator/test/test_without_response_service.chopper.dart @@ -269,7 +269,7 @@ final class _$HttpTestService extends HttpTestService { } @override - Future deleteTest(String id) async { + Future deleteTest(String id) async { final Uri $url = Uri.parse('/test/delete/${id}'); final Map $headers = { 'foo': 'bar', @@ -280,7 +280,7 @@ final class _$HttpTestService extends HttpTestService { client.baseUrl, headers: $headers, ); - final Response $response = await client.send($request); + final Response $response = await client.send($request); return $response.bodyOrThrow; } diff --git a/chopper_generator/test/test_without_response_service.dart b/chopper_generator/test/test_without_response_service.dart index 1a0ac821..d8a95417 100644 --- a/chopper_generator/test/test_without_response_service.dart +++ b/chopper_generator/test/test_without_response_service.dart @@ -80,7 +80,7 @@ abstract class HttpTestService extends ChopperService { Future putTest(@Path('id') String test, @Body() String data); @Delete(path: 'delete/{id}', headers: {'foo': 'bar'}) - Future deleteTest(@Path() String id); + Future deleteTest(@Path() String id); @Patch(path: 'patch/{id}') Future patchTest(@Path() String id, @Body() String data);