Skip to content
Merged
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
18 changes: 11 additions & 7 deletions lib/src/sturdy_http.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,17 @@ class SturdyHttp {
onResponse: onResponse,
);
} on Exception catch (e) {
await _onEvent(
DecodingError(
request: responsePayload.dioResponse!.requestOptions,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This null check previously caused a NPE (seen when migrating the TT dart client to this version).

exception: e,
stackTrace: StackTrace.current,
),
);
final response = responsePayload.dioResponse;
if (response != null) {
await _onEvent(
DecodingError(
request: response.requestOptions,
exception: e,
stackTrace: StackTrace.current,
),
);
}

rethrow;
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/src/sturdy_http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,36 @@ void main() {
await expectLater(request, throwsA(isA<TypeError>()));
},
);

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<Json, Result<Foo, String>>(
const GetRequest('/throws'),
onResponse: (response) {
return switch (response) {
OkResponse<Json>(:final response) => Success(
Foo.fromMap(response),
),
GenericError() => throw Exception(
'Decoding error in GenericError',
),
_ => const Failure('Not expected: orElse'),
};
},
);

await expectLater(request, throwsA(isA<Exception>()));
expect(jsonDecodingErrors, isEmpty);
},
);
});
});

Expand Down