Skip to content

Commit

Permalink
🎨 Updated exception based on feedback. Added test for void.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guldem committed Jan 4, 2024
1 parent 648cdfc commit 8790207
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 16 deletions.
13 changes: 13 additions & 0 deletions chopper/lib/src/chopper_http_exception.dart
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 9 in chopper/lib/src/chopper_http_exception.dart

View check run for this annotation

Codecov / codecov/patch

chopper/lib/src/chopper_http_exception.dart#L9

Added line #L9 was not covered by tests
String toString() {
return 'Could not fetch the response for ${response.base.request}. Status code: ${response.statusCode}, error: ${response.error}';

Check warning on line 11 in chopper/lib/src/chopper_http_exception.dart

View check run for this annotation

Codecov / codecov/patch

chopper/lib/src/chopper_http_exception.dart#L11

Added line #L11 was not covered by tests
}
}
3 changes: 2 additions & 1 deletion chopper/lib/src/response.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -87,7 +88,7 @@ base class Response<BodyType> with EquatableMixin {
if (error is Exception) {
throw error!;
}
throw HttpException('Could not fetch response $statusCode: $error');
throw ChopperHttpException(this);
}
}

Expand Down
27 changes: 18 additions & 9 deletions chopper/test/response_test.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -81,25 +80,35 @@ void main() {
final base = http.Response('Foobar', 400);
final response = Response(base, '', error: 'Error occurred');

expect(() => response.bodyOrThrow, throwsA(isA<HttpException>()));
expect(() => response.bodyOrThrow, throwsA(isA<ChopperHttpException>()));
});

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<HttpException>()));
expect(() => response.bodyOrThrow, throwsA(isA<ChopperHttpException>()));
});

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<String> response = Response(base, null);

expect(() => response.bodyOrThrow, throwsA(isA<ChopperHttpException>()));
});

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<void> response = Response(base, Null);

expect(() => response.bodyOrThrow, throwsA(isA<HttpException>()));
expect(() => response.bodyOrThrow, returnsNormally);
});
});
}
4 changes: 2 additions & 2 deletions chopper/test/test_without_response_service.chopper.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chopper/test/test_without_response_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> deleteTest(@Path() String id);

@Patch(path: 'patch/{id}')
Future patchTest(@Path() String id, @Body() String data);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chopper_generator/test/test_without_response_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> deleteTest(@Path() String id);

@Patch(path: 'patch/{id}')
Future patchTest(@Path() String id, @Body() String data);
Expand Down

0 comments on commit 8790207

Please sign in to comment.