diff --git a/lib/src/sturdy_http.dart b/lib/src/sturdy_http.dart index 6588fc8..067f74a 100644 --- a/lib/src/sturdy_http.dart +++ b/lib/src/sturdy_http.dart @@ -111,13 +111,17 @@ class SturdyHttp { onResponse: onResponse, ); } on Exception catch (e) { - await _onEvent( - DecodingError( - request: responsePayload.dioResponse!.requestOptions, - exception: e, - stackTrace: StackTrace.current, - ), - ); + final response = responsePayload.dioResponse; + if (response != null) { + await _onEvent( + DecodingError( + request: response.requestOptions, + exception: e, + stackTrace: StackTrace.current, + ), + ); + } + rethrow; } } diff --git a/test/src/sturdy_http_test.dart b/test/src/sturdy_http_test.dart index e60c7da..b56c4dd 100644 --- a/test/src/sturdy_http_test.dart +++ b/test/src/sturdy_http_test.dart @@ -320,6 +320,36 @@ void main() { await expectLater(request, throwsA(isA())); }, ); + + test( + 'it does not emit a decodingError event when response is null', + () async { + charlatan.whenGet( + '/throws', + (request) => + throw const SocketException('Connection failed'), + ); + + final request = buildSubject() + .execute>( + const GetRequest('/throws'), + onResponse: (response) { + return switch (response) { + OkResponse(:final response) => Success( + Foo.fromMap(response), + ), + GenericError() => throw Exception( + 'Decoding error in GenericError', + ), + _ => const Failure('Not expected: orElse'), + }; + }, + ); + + await expectLater(request, throwsA(isA())); + expect(jsonDecodingErrors, isEmpty); + }, + ); }); });