From b4e6538ae2b056912794971d4dca797dfbc1b95e Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Wed, 18 Oct 2023 12:39:12 +0200 Subject: [PATCH 1/2] Parse error responses for more detailed messages. --- lib/src/streaming_sync.dart | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/lib/src/streaming_sync.dart b/lib/src/streaming_sync.dart index 03ff0c79..12c2dd3b 100644 --- a/lib/src/streaming_sync.dart +++ b/lib/src/streaming_sync.dart @@ -127,7 +127,7 @@ class StreamingSyncImplementation { } } if (response.statusCode != 200) { - throw HttpException(response.reasonPhrase ?? "Request failed", uri: uri); + throw getError(response); } final body = convert.jsonDecode(response.body); @@ -294,8 +294,7 @@ class StreamingSyncImplementation { } } if (res.statusCode != 200) { - throw HttpException(res.reasonPhrase ?? 'Invalid http response', - uri: uri); + throw await getStreamedError(res); } // Note: The response stream is automatically closed when this loop errors @@ -304,3 +303,29 @@ class StreamingSyncImplementation { } } } + +HttpException getError(http.Response response) { + try { + final body = response.body; + final decoded = convert.jsonDecode(body); + final details = decoded['error']?['details']?[0] ?? body; + final message = '${response.reasonPhrase ?? "Request failed"}: ${details}'; + return HttpException(message, uri: response.request?.url); + } on Error catch (_) { + return HttpException(response.reasonPhrase ?? "Request failed", + uri: response.request?.url); + } +} + +Future getStreamedError(http.StreamedResponse response) async { + try { + final body = await response.stream.bytesToString(); + final decoded = convert.jsonDecode(body); + final details = decoded['error']?['details']?[0] ?? body; + final message = '${response.reasonPhrase ?? "Request failed"}: $details'; + return HttpException(message, uri: response.request?.url); + } on Error catch (_) { + return HttpException(response.reasonPhrase ?? "Request failed", + uri: response.request?.url); + } +} From 393fb7ddba268f082acdc578da5178f46d1d5893 Mon Sep 17 00:00:00 2001 From: Ralf Kistner Date: Tue, 14 Nov 2023 11:31:56 +0200 Subject: [PATCH 2/2] Fix lint issues. --- lib/src/streaming_sync.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/streaming_sync.dart b/lib/src/streaming_sync.dart index 12c2dd3b..ed217521 100644 --- a/lib/src/streaming_sync.dart +++ b/lib/src/streaming_sync.dart @@ -309,7 +309,7 @@ HttpException getError(http.Response response) { final body = response.body; final decoded = convert.jsonDecode(body); final details = decoded['error']?['details']?[0] ?? body; - final message = '${response.reasonPhrase ?? "Request failed"}: ${details}'; + final message = '${response.reasonPhrase ?? "Request failed"}: $details'; return HttpException(message, uri: response.request?.url); } on Error catch (_) { return HttpException(response.reasonPhrase ?? "Request failed",