diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index 0479c2a..12eff83 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -1,6 +1,7 @@ // ignore_for_file: public_member_api_docs import 'package:dio/dio.dart'; +import 'package:equatable/equatable.dart'; /// The produced object after [SturdyHttp] processes a [NetworkRequest]. /// @@ -10,7 +11,7 @@ import 'package:dio/dio.dart'; /// Most often the call sites executing a [NetworkRequest] will only be interested /// in a select few of these, and will resolve their error cases via a `maybeWhen` /// using the `orElse` clause. -sealed class NetworkResponse { +sealed class NetworkResponse extends Equatable { const NetworkResponse(); } @@ -20,12 +21,18 @@ sealed class NetworkResponseSuccess extends NetworkResponse { final class OkNoContent extends NetworkResponseSuccess { const OkNoContent(); + + @override + List get props => []; } final class OkResponse extends NetworkResponseSuccess { final T response; const OkResponse(this.response); + + @override + List get props => [response]; } sealed class NetworkResponseFailure extends NetworkResponse { @@ -37,6 +44,9 @@ final class Unauthorized extends NetworkResponseFailure { final DioException error; const Unauthorized({required this.error}); + + @override + List get props => [error]; } /// 403 - for responses when the request was authenticated but the @@ -45,6 +55,9 @@ final class Forbidden extends NetworkResponseFailure { final DioException error; const Forbidden({required this.error}); + + @override + List get props => [error]; } /// 404 - for responses when we could not locate a resource, or when @@ -53,6 +66,9 @@ final class NotFound extends NetworkResponseFailure { final DioException error; const NotFound({required this.error}); + + @override + List get props => [error]; } /// 422 - for responses when the request inputs failed our validations. @@ -61,6 +77,9 @@ final class UnprocessableEntity extends NetworkResponseFailure { final R response; const UnprocessableEntity({required this.error, required this.response}); + + @override + List get props => [error, response]; } /// 426 - for responses when a client version upgrade is required @@ -68,6 +87,9 @@ final class UpgradeRequired extends NetworkResponseFailure { final DioException error; const UpgradeRequired({required this.error}); + + @override + List get props => [error]; } /// 500 - for responses where the service had an error while processing @@ -76,6 +98,9 @@ final class ServerError extends NetworkResponseFailure { final DioException error; const ServerError({required this.error}); + + @override + List get props => [error]; } /// 503 - for responses when an underlying service issue prevents us from @@ -84,6 +109,9 @@ final class ServiceUnavailable extends NetworkResponseFailure { final DioException error; const ServiceUnavailable({required this.error}); + + @override + List get props => [error]; } /// Any "other" error not covered by the above cases. If a [DioException] is present, @@ -99,6 +127,9 @@ final class GenericError extends NetworkResponseFailure { final DioException? error; final String message; final bool isConnectionIssue; + + @override + List get props => [error, message, isConnectionIssue]; } /// Extensions on the [NetworkResponse] type diff --git a/pubspec.yaml b/pubspec.yaml index 4b5d4eb..470c7db 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: collection: ^1.18.0 dio: ^5.7.0 + equatable: ^2.0.5 freezed_annotation: ^2.4.4 uuid: ^4.5.0 diff --git a/test/src/network_response_test.dart b/test/src/network_response_test.dart new file mode 100644 index 0000000..73dfa64 --- /dev/null +++ b/test/src/network_response_test.dart @@ -0,0 +1,29 @@ +import 'package:dio/dio.dart'; +import 'package:sturdy_http/sturdy_http.dart'; +import 'package:test/test.dart'; + +void main() { + group('NetworkResponse', () { + test('it has a meaningful toString implementation', () { + final subject = GenericError( + message: 'Oh no! Something went wrong', + isConnectionIssue: true, + error: DioException( + type: DioExceptionType.unknown, + requestOptions: RequestOptions(path: 'https://example.com'), + message: 'Blah blah blah', + response: Response( + requestOptions: RequestOptions(path: 'https://example.com'), + statusCode: 500, + statusMessage: 'Internal Server Error', + ), + ), + ); + + expect( + subject.toString(), + 'GenericError(DioException [unknown]: Blah blah blah, Oh no! Something went wrong, true)', + ); + }); + }); +}