@@ -38,39 +38,41 @@ class SyncResponseException implements Exception {
38
38
http.StreamedResponse response) async {
39
39
try {
40
40
final body = await response.stream.bytesToString ();
41
- final decoded = convert.jsonDecode (body);
42
- final details = _stringOrFirst (decoded['error' ]? ['details' ]) ?? body;
43
- final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
44
- return SyncResponseException (response.statusCode, message);
45
- } on Error catch (_) {
46
- return SyncResponseException (
47
- response.statusCode,
48
- response.reasonPhrase ?? "Request failed" ,
49
- );
41
+ return _fromResponseBody (response, body);
42
+ } on Exception catch (_) {
43
+ // Could be FormatException, stream issues, or possibly other exceptions.
44
+ // Fallback to just using the response header.
45
+ return _fromResponseHeader (response);
50
46
}
51
47
}
52
48
53
49
/// Parse an error response from the PowerSync service
54
50
static SyncResponseException fromResponse (http.Response response) {
55
51
try {
56
52
final body = response.body;
57
- final decoded = convert.jsonDecode (body);
58
- final details = _stringOrFirst (decoded['error' ]? ['details' ]) ?? body;
59
- final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
60
- return SyncResponseException (response.statusCode, message);
61
- } on FormatException catch (_) {
62
- return SyncResponseException (
63
- response.statusCode,
64
- response.reasonPhrase ?? "Request failed" ,
65
- );
66
- } on Error catch (_) {
67
- return SyncResponseException (
68
- response.statusCode,
69
- response.reasonPhrase ?? "Request failed" ,
70
- );
53
+ return _fromResponseBody (response, body);
54
+ } on Exception catch (_) {
55
+ // Could be FormatException, or possibly other exceptions.
56
+ // Fallback to just using the response header.
57
+ return _fromResponseHeader (response);
71
58
}
72
59
}
73
60
61
+ static SyncResponseException _fromResponseBody (
62
+ http.BaseResponse response, String body) {
63
+ final decoded = convert.jsonDecode (body);
64
+ final details = _stringOrFirst (decoded['error' ]? ['details' ]) ?? body;
65
+ final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
66
+ return SyncResponseException (response.statusCode, message);
67
+ }
68
+
69
+ static SyncResponseException _fromResponseHeader (http.BaseResponse response) {
70
+ return SyncResponseException (
71
+ response.statusCode,
72
+ response.reasonPhrase ?? "Request failed" ,
73
+ );
74
+ }
75
+
74
76
int statusCode;
75
77
String description;
76
78
0 commit comments