@@ -38,34 +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);
41
+ return _fromResponseBody (response, body);
45
42
} on Exception catch (_) {
46
- return SyncResponseException (
47
- response.statusCode,
48
- response.reasonPhrase ?? "Request failed" ,
49
- );
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);
53
+ return _fromResponseBody (response, body);
61
54
} on Exception catch (_) {
62
- return SyncResponseException (
63
- response.statusCode,
64
- response.reasonPhrase ?? "Request failed" ,
65
- );
55
+ // Could be FormatException, or possibly other exceptions.
56
+ // Fallback to just using the response header.
57
+ return _fromResponseHeader (response);
66
58
}
67
59
}
68
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
+
69
76
int statusCode;
70
77
String description;
71
78
0 commit comments