diff --git a/lib/src/streaming_sync.dart b/lib/src/streaming_sync.dart index 03ff0c79..ed217521 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); + } +}