From 5afe3d3e354f28650b185ecd728ec0df67734743 Mon Sep 17 00:00:00 2001 From: Rene Floor Date: Tue, 11 Nov 2025 15:56:42 +0100 Subject: [PATCH 1/4] fix error while parsing unknown body --- packages/stream_core/CHANGELOG.md | 6 ++++++ packages/stream_core/lib/src/api/stream_core_dio_error.dart | 6 +++++- packages/stream_core/pubspec.yaml | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/stream_core/CHANGELOG.md b/packages/stream_core/CHANGELOG.md index af6f52c..17aec84 100644 --- a/packages/stream_core/CHANGELOG.md +++ b/packages/stream_core/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.3.1 + +### 🐛 Bug Fixes + +- Fixed `StreamDioException.toClientException()` not handling invalid JSON strings gracefully + ## 0.3.0 ### 💥 BREAKING CHANGES diff --git a/packages/stream_core/lib/src/api/stream_core_dio_error.dart b/packages/stream_core/lib/src/api/stream_core_dio_error.dart index 66feb70..ba741ea 100644 --- a/packages/stream_core/lib/src/api/stream_core_dio_error.dart +++ b/packages/stream_core/lib/src/api/stream_core_dio_error.dart @@ -28,7 +28,11 @@ extension StreamDioExceptionExtension on DioException { if (data is Map) { apiError = StreamApiError.fromJson(data); } else if (data is String) { - apiError = StreamApiError.fromJson(jsonDecode(data)); + try { + apiError = StreamApiError.fromJson(jsonDecode(data)); + } catch (e) { + apiError = null; + } } return HttpClientException( message: apiError?.message ?? response?.statusMessage ?? message ?? '', diff --git a/packages/stream_core/pubspec.yaml b/packages/stream_core/pubspec.yaml index c2fbef2..41c0a15 100644 --- a/packages/stream_core/pubspec.yaml +++ b/packages/stream_core/pubspec.yaml @@ -1,6 +1,6 @@ name: stream_core description: "Internal sdk with low-level utilities for the stream SDKs" -version: 0.3.0 +version: 0.3.1 repository: https://github.com/GetStream/stream-core-flutter # Note: The environment configuration and dependency versions are managed by Melos. From 21bdb04fb62419fb510a1fcac15c8e0fcd68719d Mon Sep 17 00:00:00 2001 From: Rene Floor Date: Tue, 11 Nov 2025 16:03:31 +0100 Subject: [PATCH 2/4] Also wrap first parsing for apiError with catch --- .../lib/src/api/stream_core_dio_error.dart | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/stream_core/lib/src/api/stream_core_dio_error.dart b/packages/stream_core/lib/src/api/stream_core_dio_error.dart index ba741ea..75aba19 100644 --- a/packages/stream_core/lib/src/api/stream_core_dio_error.dart +++ b/packages/stream_core/lib/src/api/stream_core_dio_error.dart @@ -25,14 +25,15 @@ extension StreamDioExceptionExtension on DioException { final response = this.response; StreamApiError? apiError; final data = response?.data; - if (data is Map) { - apiError = StreamApiError.fromJson(data); - } else if (data is String) { - try { + + try { + if (data is Map) { + apiError = StreamApiError.fromJson(data); + } else if (data is String) { apiError = StreamApiError.fromJson(jsonDecode(data)); - } catch (e) { - apiError = null; } + } catch (e) { + apiError = null; } return HttpClientException( message: apiError?.message ?? response?.statusMessage ?? message ?? '', From 24705fdf75748441f5102963f944161d291ba5f6 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 3 Dec 2025 03:26:56 +0100 Subject: [PATCH 3/4] refactor: Safely parse StreamApiError from DioException Refactors the `toClientException` extension on `DioException` to more safely parse the `StreamApiError`. The new implementation uses `runSafelySync` and a switch expression to handle different response data types (Map or String) when creating a `StreamApiError` from JSON, replacing the previous `try-catch` block. --- .../lib/src/api/stream_core_dio_error.dart | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/stream_core/lib/src/api/stream_core_dio_error.dart b/packages/stream_core/lib/src/api/stream_core_dio_error.dart index 75aba19..33cf78b 100644 --- a/packages/stream_core/lib/src/api/stream_core_dio_error.dart +++ b/packages/stream_core/lib/src/api/stream_core_dio_error.dart @@ -22,19 +22,16 @@ class StreamDioException extends DioException { extension StreamDioExceptionExtension on DioException { HttpClientException toClientException() { - final response = this.response; - StreamApiError? apiError; - final data = response?.data; + final apiErrorResult = runSafelySync( + () => switch (response?.data) { + final Map data => StreamApiError.fromJson(data), + final String data => StreamApiError.fromJson(jsonDecode(data)), + _ => null, + }, + ); + + final apiError = apiErrorResult.getOrNull(); - try { - if (data is Map) { - apiError = StreamApiError.fromJson(data); - } else if (data is String) { - apiError = StreamApiError.fromJson(jsonDecode(data)); - } - } catch (e) { - apiError = null; - } return HttpClientException( message: apiError?.message ?? response?.statusMessage ?? message ?? '', error: apiError ?? this, From a8b538100b322698242222409a5388e569fcbdd5 Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 3 Dec 2025 03:28:00 +0100 Subject: [PATCH 4/4] chore: remove version update --- packages/stream_core/CHANGELOG.md | 2 +- packages/stream_core/pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/stream_core/CHANGELOG.md b/packages/stream_core/CHANGELOG.md index 17aec84..50988f5 100644 --- a/packages/stream_core/CHANGELOG.md +++ b/packages/stream_core/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.3.1 +## Upcoming ### 🐛 Bug Fixes diff --git a/packages/stream_core/pubspec.yaml b/packages/stream_core/pubspec.yaml index 41c0a15..c2fbef2 100644 --- a/packages/stream_core/pubspec.yaml +++ b/packages/stream_core/pubspec.yaml @@ -1,6 +1,6 @@ name: stream_core description: "Internal sdk with low-level utilities for the stream SDKs" -version: 0.3.1 +version: 0.3.0 repository: https://github.com/GetStream/stream-core-flutter # Note: The environment configuration and dependency versions are managed by Melos.