Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: toString implementation on NetworkResponse #18

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/src/network_response.dart
Original file line number Diff line number Diff line change
@@ -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].
///
Expand All @@ -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<R> {
sealed class NetworkResponse<R> extends Equatable {
const NetworkResponse();
}

Expand All @@ -20,12 +21,18 @@ sealed class NetworkResponseSuccess<R> extends NetworkResponse<R> {

final class OkNoContent<R> extends NetworkResponseSuccess<R> {
const OkNoContent();

@override
List<Object?> get props => [];
}

final class OkResponse<T> extends NetworkResponseSuccess<T> {
final T response;

const OkResponse(this.response);

@override
List<Object?> get props => [response];
}

sealed class NetworkResponseFailure<R> extends NetworkResponse<R> {
Expand All @@ -37,6 +44,9 @@ final class Unauthorized<R> extends NetworkResponseFailure<R> {
final DioException error;

const Unauthorized({required this.error});

@override
List<Object?> get props => [error];
}

/// 403 - for responses when the request was authenticated but the
Expand All @@ -45,6 +55,9 @@ final class Forbidden<R> extends NetworkResponseFailure<R> {
final DioException error;

const Forbidden({required this.error});

@override
List<Object?> get props => [error];
}

/// 404 - for responses when we could not locate a resource, or when
Expand All @@ -53,6 +66,9 @@ final class NotFound<R> extends NetworkResponseFailure<R> {
final DioException error;

const NotFound({required this.error});

@override
List<Object?> get props => [error];
}

/// 422 - for responses when the request inputs failed our validations.
Expand All @@ -61,13 +77,19 @@ final class UnprocessableEntity<R> extends NetworkResponseFailure<R> {
final R response;

const UnprocessableEntity({required this.error, required this.response});

@override
List<Object?> get props => [error, response];
}

/// 426 - for responses when a client version upgrade is required
final class UpgradeRequired<R> extends NetworkResponseFailure<R> {
final DioException error;

const UpgradeRequired({required this.error});

@override
List<Object?> get props => [error];
}

/// 500 - for responses where the service had an error while processing
Expand All @@ -76,6 +98,9 @@ final class ServerError<R> extends NetworkResponseFailure<R> {
final DioException error;

const ServerError({required this.error});

@override
List<Object?> get props => [error];
}

/// 503 - for responses when an underlying service issue prevents us from
Expand All @@ -84,6 +109,9 @@ final class ServiceUnavailable<R> extends NetworkResponseFailure<R> {
final DioException error;

const ServiceUnavailable({required this.error});

@override
List<Object?> get props => [error];
}

/// Any "other" error not covered by the above cases. If a [DioException] is present,
Expand All @@ -99,6 +127,9 @@ final class GenericError<R> extends NetworkResponseFailure<R> {
final DioException? error;
final String message;
final bool isConnectionIssue;

@override
List<Object?> get props => [error, message, isConnectionIssue];
}

/// Extensions on the [NetworkResponse] type
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions test/src/network_response_test.dart
Original file line number Diff line number Diff line change
@@ -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<dynamic>(DioException [unknown]: Blah blah blah, Oh no! Something went wrong, true)',
);
});
});
}
Loading