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
6 changes: 6 additions & 0 deletions packages/stream_core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Upcoming

### 🐛 Bug Fixes

- Fixed `StreamDioException.toClientException()` not handling invalid JSON strings gracefully

## 0.3.0

### 💥 BREAKING CHANGES
Expand Down
18 changes: 10 additions & 8 deletions packages/stream_core/lib/src/api/stream_core_dio_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ class StreamDioException extends DioException {

extension StreamDioExceptionExtension on DioException {
HttpClientException toClientException() {
final response = this.response;
StreamApiError? apiError;
final data = response?.data;
if (data is Map<String, Object?>) {
apiError = StreamApiError.fromJson(data);
} else if (data is String) {
apiError = StreamApiError.fromJson(jsonDecode(data));
}
final apiErrorResult = runSafelySync(
() => switch (response?.data) {
final Map<String, Object?> data => StreamApiError.fromJson(data),
final String data => StreamApiError.fromJson(jsonDecode(data)),
_ => null,
},
);

final apiError = apiErrorResult.getOrNull();

return HttpClientException(
message: apiError?.message ?? response?.statusMessage ?? message ?? '',
error: apiError ?? this,
Expand Down