From 1b9fd41ad066839b7a6c700a758bb1901d55f8cb Mon Sep 17 00:00:00 2001 From: willlockwood Date: Mon, 22 Jul 2024 09:04:47 -0700 Subject: [PATCH 1/4] feat: Add support for 426: Upgrade Required --- lib/src/network_response.dart | 3 + lib/src/network_response.freezed.dart | 251 ++++++++++++++++++++++++++ lib/src/sturdy_http.dart | 3 + test/src/sturdy_http_test.dart | 27 +++ 4 files changed, 284 insertions(+) diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index e8249e8..f33030d 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -38,6 +38,9 @@ class NetworkResponse with _$NetworkResponse { required R response, }) = _UnprocessableEntity; + /// 426 - for responses when access to a resource requires a client upgrade. + const factory NetworkResponse.upgradeRequired(DioException error) = _UpgradeRequired; + /// 500 - for responses where the service had an error while processing /// the request. const factory NetworkResponse.serverError(DioException error) = _ServerError; diff --git a/lib/src/network_response.freezed.dart b/lib/src/network_response.freezed.dart index c5ec2ab..45a7989 100644 --- a/lib/src/network_response.freezed.dart +++ b/lib/src/network_response.freezed.dart @@ -25,6 +25,7 @@ mixin _$NetworkResponse { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -40,6 +41,7 @@ mixin _$NetworkResponse { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -55,6 +57,7 @@ mixin _$NetworkResponse { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -72,6 +75,7 @@ mixin _$NetworkResponse { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -85,6 +89,7 @@ mixin _$NetworkResponse { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -98,6 +103,7 @@ mixin _$NetworkResponse { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -195,6 +201,7 @@ class _$OkImpl implements _Ok { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -213,6 +220,7 @@ class _$OkImpl implements _Ok { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -231,6 +239,7 @@ class _$OkImpl implements _Ok { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -254,6 +263,7 @@ class _$OkImpl implements _Ok { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -270,6 +280,7 @@ class _$OkImpl implements _Ok { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -286,6 +297,7 @@ class _$OkImpl implements _Ok { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -352,6 +364,7 @@ class _$OkNoContentImpl implements _OkNoContent { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -370,6 +383,7 @@ class _$OkNoContentImpl implements _OkNoContent { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -388,6 +402,7 @@ class _$OkNoContentImpl implements _OkNoContent { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -411,6 +426,7 @@ class _$OkNoContentImpl implements _OkNoContent { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -427,6 +443,7 @@ class _$OkNoContentImpl implements _OkNoContent { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -443,6 +460,7 @@ class _$OkNoContentImpl implements _OkNoContent { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -531,6 +549,7 @@ class _$UnauthorizedImpl implements _Unauthorized { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -549,6 +568,7 @@ class _$UnauthorizedImpl implements _Unauthorized { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -567,6 +587,7 @@ class _$UnauthorizedImpl implements _Unauthorized { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -590,6 +611,7 @@ class _$UnauthorizedImpl implements _Unauthorized { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -606,6 +628,7 @@ class _$UnauthorizedImpl implements _Unauthorized { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -622,6 +645,7 @@ class _$UnauthorizedImpl implements _Unauthorized { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -714,6 +738,7 @@ class _$ForbiddenImpl implements _Forbidden { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -732,6 +757,7 @@ class _$ForbiddenImpl implements _Forbidden { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -750,6 +776,7 @@ class _$ForbiddenImpl implements _Forbidden { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -773,6 +800,7 @@ class _$ForbiddenImpl implements _Forbidden { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -789,6 +817,7 @@ class _$ForbiddenImpl implements _Forbidden { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -805,6 +834,7 @@ class _$ForbiddenImpl implements _Forbidden { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -897,6 +927,7 @@ class _$NotFoundImpl implements _NotFound { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -915,6 +946,7 @@ class _$NotFoundImpl implements _NotFound { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -933,6 +965,7 @@ class _$NotFoundImpl implements _NotFound { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -956,6 +989,7 @@ class _$NotFoundImpl implements _NotFound { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -972,6 +1006,7 @@ class _$NotFoundImpl implements _NotFound { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -988,6 +1023,7 @@ class _$NotFoundImpl implements _NotFound { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -1091,6 +1127,7 @@ class _$UnprocessableEntityImpl implements _UnprocessableEntity { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -1109,6 +1146,7 @@ class _$UnprocessableEntityImpl implements _UnprocessableEntity { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -1127,6 +1165,7 @@ class _$UnprocessableEntityImpl implements _UnprocessableEntity { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -1150,6 +1189,7 @@ class _$UnprocessableEntityImpl implements _UnprocessableEntity { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -1166,6 +1206,7 @@ class _$UnprocessableEntityImpl implements _UnprocessableEntity { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -1182,6 +1223,7 @@ class _$UnprocessableEntityImpl implements _UnprocessableEntity { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -1206,6 +1248,197 @@ abstract class _UnprocessableEntity implements NetworkResponse { get copyWith => throw _privateConstructorUsedError; } +/// @nodoc +abstract class _$$UpgradeRequiredImplCopyWith { + factory _$$UpgradeRequiredImplCopyWith(_$UpgradeRequiredImpl value, + $Res Function(_$UpgradeRequiredImpl) then) = + __$$UpgradeRequiredImplCopyWithImpl; + @useResult + $Res call({DioException error}); +} + +/// @nodoc +class __$$UpgradeRequiredImplCopyWithImpl + extends _$NetworkResponseCopyWithImpl> + implements _$$UpgradeRequiredImplCopyWith { + __$$UpgradeRequiredImplCopyWithImpl(_$UpgradeRequiredImpl _value, + $Res Function(_$UpgradeRequiredImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? error = null, + }) { + return _then(_$UpgradeRequiredImpl( + null == error + ? _value.error + : error // ignore: cast_nullable_to_non_nullable + as DioException, + )); + } +} + +/// @nodoc + +class _$UpgradeRequiredImpl implements _UpgradeRequired { + const _$UpgradeRequiredImpl(this.error); + + @override + final DioException error; + + @override + String toString() { + return 'NetworkResponse<$R>.upgradeRequired(error: $error)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$UpgradeRequiredImpl && + (identical(other.error, error) || other.error == error)); + } + + @override + int get hashCode => Object.hash(runtimeType, error); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$UpgradeRequiredImplCopyWith> get copyWith => + __$$UpgradeRequiredImplCopyWithImpl>( + this, _$identity); + + @override + @optionalTypeArgs + TResult when({ + required TResult Function(R response) ok, + required TResult Function() okNoContent, + required TResult Function(DioException error) unauthorized, + required TResult Function(DioException error) forbidden, + required TResult Function(DioException error) notFound, + required TResult Function(DioException error, R response) + unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, + required TResult Function(DioException error) serverError, + required TResult Function(DioException error) serviceUnavailable, + required TResult Function( + String message, bool isConnectionIssue, DioException? error) + genericError, + }) { + return upgradeRequired(error); + } + + @override + @optionalTypeArgs + TResult? whenOrNull({ + TResult? Function(R response)? ok, + TResult? Function()? okNoContent, + TResult? Function(DioException error)? unauthorized, + TResult? Function(DioException error)? forbidden, + TResult? Function(DioException error)? notFound, + TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, + TResult? Function(DioException error)? serverError, + TResult? Function(DioException error)? serviceUnavailable, + TResult? Function( + String message, bool isConnectionIssue, DioException? error)? + genericError, + }) { + return upgradeRequired?.call(error); + } + + @override + @optionalTypeArgs + TResult maybeWhen({ + TResult Function(R response)? ok, + TResult Function()? okNoContent, + TResult Function(DioException error)? unauthorized, + TResult Function(DioException error)? forbidden, + TResult Function(DioException error)? notFound, + TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, + TResult Function(DioException error)? serverError, + TResult Function(DioException error)? serviceUnavailable, + TResult Function( + String message, bool isConnectionIssue, DioException? error)? + genericError, + required TResult orElse(), + }) { + if (upgradeRequired != null) { + return upgradeRequired(error); + } + return orElse(); + } + + @override + @optionalTypeArgs + TResult map({ + required TResult Function(_Ok value) ok, + required TResult Function(_OkNoContent value) okNoContent, + required TResult Function(_Unauthorized value) unauthorized, + required TResult Function(_Forbidden value) forbidden, + required TResult Function(_NotFound value) notFound, + required TResult Function(_UnprocessableEntity value) + unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, + required TResult Function(_ServerError value) serverError, + required TResult Function(_ServiceUnavailable value) serviceUnavailable, + required TResult Function(_GenericError value) genericError, + }) { + return upgradeRequired(this); + } + + @override + @optionalTypeArgs + TResult? mapOrNull({ + TResult? Function(_Ok value)? ok, + TResult? Function(_OkNoContent value)? okNoContent, + TResult? Function(_Unauthorized value)? unauthorized, + TResult? Function(_Forbidden value)? forbidden, + TResult? Function(_NotFound value)? notFound, + TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, + TResult? Function(_ServerError value)? serverError, + TResult? Function(_ServiceUnavailable value)? serviceUnavailable, + TResult? Function(_GenericError value)? genericError, + }) { + return upgradeRequired?.call(this); + } + + @override + @optionalTypeArgs + TResult maybeMap({ + TResult Function(_Ok value)? ok, + TResult Function(_OkNoContent value)? okNoContent, + TResult Function(_Unauthorized value)? unauthorized, + TResult Function(_Forbidden value)? forbidden, + TResult Function(_NotFound value)? notFound, + TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, + TResult Function(_ServerError value)? serverError, + TResult Function(_ServiceUnavailable value)? serviceUnavailable, + TResult Function(_GenericError value)? genericError, + required TResult orElse(), + }) { + if (upgradeRequired != null) { + return upgradeRequired(this); + } + return orElse(); + } +} + +abstract class _UpgradeRequired implements NetworkResponse { + const factory _UpgradeRequired(final DioException error) = + _$UpgradeRequiredImpl; + + DioException get error; + @JsonKey(ignore: true) + _$$UpgradeRequiredImplCopyWith> get copyWith => + throw _privateConstructorUsedError; +} + /// @nodoc abstract class _$$ServerErrorImplCopyWith { factory _$$ServerErrorImplCopyWith(_$ServerErrorImpl value, @@ -1278,6 +1511,7 @@ class _$ServerErrorImpl implements _ServerError { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -1296,6 +1530,7 @@ class _$ServerErrorImpl implements _ServerError { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -1314,6 +1549,7 @@ class _$ServerErrorImpl implements _ServerError { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -1337,6 +1573,7 @@ class _$ServerErrorImpl implements _ServerError { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -1353,6 +1590,7 @@ class _$ServerErrorImpl implements _ServerError { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -1369,6 +1607,7 @@ class _$ServerErrorImpl implements _ServerError { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -1462,6 +1701,7 @@ class _$ServiceUnavailableImpl implements _ServiceUnavailable { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -1480,6 +1720,7 @@ class _$ServiceUnavailableImpl implements _ServiceUnavailable { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -1498,6 +1739,7 @@ class _$ServiceUnavailableImpl implements _ServiceUnavailable { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -1521,6 +1763,7 @@ class _$ServiceUnavailableImpl implements _ServiceUnavailable { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -1537,6 +1780,7 @@ class _$ServiceUnavailableImpl implements _ServiceUnavailable { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -1553,6 +1797,7 @@ class _$ServiceUnavailableImpl implements _ServiceUnavailable { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, @@ -1666,6 +1911,7 @@ class _$GenericErrorImpl implements _GenericError { required TResult Function(DioException error) notFound, required TResult Function(DioException error, R response) unprocessableEntity, + required TResult Function(DioException error) upgradeRequired, required TResult Function(DioException error) serverError, required TResult Function(DioException error) serviceUnavailable, required TResult Function( @@ -1684,6 +1930,7 @@ class _$GenericErrorImpl implements _GenericError { TResult? Function(DioException error)? forbidden, TResult? Function(DioException error)? notFound, TResult? Function(DioException error, R response)? unprocessableEntity, + TResult? Function(DioException error)? upgradeRequired, TResult? Function(DioException error)? serverError, TResult? Function(DioException error)? serviceUnavailable, TResult? Function( @@ -1702,6 +1949,7 @@ class _$GenericErrorImpl implements _GenericError { TResult Function(DioException error)? forbidden, TResult Function(DioException error)? notFound, TResult Function(DioException error, R response)? unprocessableEntity, + TResult Function(DioException error)? upgradeRequired, TResult Function(DioException error)? serverError, TResult Function(DioException error)? serviceUnavailable, TResult Function( @@ -1725,6 +1973,7 @@ class _$GenericErrorImpl implements _GenericError { required TResult Function(_NotFound value) notFound, required TResult Function(_UnprocessableEntity value) unprocessableEntity, + required TResult Function(_UpgradeRequired value) upgradeRequired, required TResult Function(_ServerError value) serverError, required TResult Function(_ServiceUnavailable value) serviceUnavailable, required TResult Function(_GenericError value) genericError, @@ -1741,6 +1990,7 @@ class _$GenericErrorImpl implements _GenericError { TResult? Function(_Forbidden value)? forbidden, TResult? Function(_NotFound value)? notFound, TResult? Function(_UnprocessableEntity value)? unprocessableEntity, + TResult? Function(_UpgradeRequired value)? upgradeRequired, TResult? Function(_ServerError value)? serverError, TResult? Function(_ServiceUnavailable value)? serviceUnavailable, TResult? Function(_GenericError value)? genericError, @@ -1757,6 +2007,7 @@ class _$GenericErrorImpl implements _GenericError { TResult Function(_Forbidden value)? forbidden, TResult Function(_NotFound value)? notFound, TResult Function(_UnprocessableEntity value)? unprocessableEntity, + TResult Function(_UpgradeRequired value)? upgradeRequired, TResult Function(_ServerError value)? serverError, TResult Function(_ServiceUnavailable value)? serviceUnavailable, TResult Function(_GenericError value)? genericError, diff --git a/lib/src/sturdy_http.dart b/lib/src/sturdy_http.dart index bd2a511..7f48491 100644 --- a/lib/src/sturdy_http.dart +++ b/lib/src/sturdy_http.dart @@ -194,6 +194,9 @@ class SturdyHttp { response: error.response?.data as R, ); break; + case 426: + resolvedResponse = NetworkResponse.upgradeRequired(error); + break; case 500: resolvedResponse = NetworkResponse.serverError(error); break; diff --git a/test/src/sturdy_http_test.dart b/test/src/sturdy_http_test.dart index 8b9dd92..9136678 100644 --- a/test/src/sturdy_http_test.dart +++ b/test/src/sturdy_http_test.dart @@ -710,6 +710,33 @@ void main() { }); }); + group('when status code is 426', () { + setUp(() { + setupErrorResponse( + statusCode: 426, + body: const Foo(message: 'error').toJson(), + ); + }); + + test('it returns upgradeRequired', () async { + final response = + await buildSubject().execute>( + const GetRequest(defaultPath), + onResponse: (response) { + return response.maybeWhen( + upgradeRequired: (error) => const Result.success(true), + orElse: () => const Result.failure('Not expected: orElse'), + ); + }, + ); + + response.when( + success: (s) => expect(s, isTrue), + failure: fail, + ); + }); + }); + group('when status code is 500', () { setUp(() { setupErrorResponse(statusCode: 500); From fcf8b1f7354b4422583660bae426fee251c263a6 Mon Sep 17 00:00:00 2001 From: willlockwood Date: Mon, 22 Jul 2024 09:11:15 -0700 Subject: [PATCH 2/4] Reformat --- lib/src/network_response.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/src/network_response.dart b/lib/src/network_response.dart index f33030d..3ea7645 100644 --- a/lib/src/network_response.dart +++ b/lib/src/network_response.dart @@ -39,7 +39,8 @@ class NetworkResponse with _$NetworkResponse { }) = _UnprocessableEntity; /// 426 - for responses when access to a resource requires a client upgrade. - const factory NetworkResponse.upgradeRequired(DioException error) = _UpgradeRequired; + const factory NetworkResponse.upgradeRequired(DioException error) = + _UpgradeRequired; /// 500 - for responses where the service had an error while processing /// the request. From e36cedcf600393b80e81307e89e6aa0a46f28e10 Mon Sep 17 00:00:00 2001 From: willlockwood Date: Mon, 22 Jul 2024 09:39:52 -0700 Subject: [PATCH 3/4] Add library-level doc comment and re-order library exports --- lib/sturdy_http.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/sturdy_http.dart b/lib/sturdy_http.dart index 2ae67b3..1f15dcc 100644 --- a/lib/sturdy_http.dart +++ b/lib/sturdy_http.dart @@ -1,8 +1,9 @@ +/// A strongly typed, event-based, reliable HTTP client that wraps Dio. library sturdy_http; export 'src/_dio_error.dart'; -export 'src/sturdy_http.dart'; -export 'src/sturdy_http_event_listener.dart'; export 'src/network_request.dart'; export 'src/network_response.dart'; export 'src/retry_behavior.dart'; +export 'src/sturdy_http.dart'; +export 'src/sturdy_http_event_listener.dart'; From a96f6ec65299d608f87ef92e9db969214e86afcb Mon Sep 17 00:00:00 2001 From: willlockwood Date: Mon, 22 Jul 2024 09:54:51 -0700 Subject: [PATCH 4/4] Upgrade min dio version to 5.2.0, which has DioException --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 3a6d8de..c6eee93 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,7 +9,7 @@ environment: dependencies: collection: ^1.17.1 - dio: ^5.1.1 + dio: ^5.2.0 freezed_annotation: ^2.2.0 uuid: ^4.1.0