Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions packages/powersync_core/lib/src/exceptions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,41 @@ class SyncResponseException implements Exception {
http.StreamedResponse response) async {
try {
final body = await response.stream.bytesToString();
final decoded = convert.jsonDecode(body);
final details = _stringOrFirst(decoded['error']?['details']) ?? body;
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
return SyncResponseException(response.statusCode, message);
} on Error catch (_) {
return SyncResponseException(
response.statusCode,
response.reasonPhrase ?? "Request failed",
);
return _fromResponseBody(response, body);
} on Exception catch (_) {
// Could be FormatException, stream issues, or possibly other exceptions.
// Fallback to just using the response header.
return _fromResponseHeader(response);
}
}

/// Parse an error response from the PowerSync service
static SyncResponseException fromResponse(http.Response response) {
try {
final body = response.body;
final decoded = convert.jsonDecode(body);
final details = _stringOrFirst(decoded['error']?['details']) ?? body;
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
return SyncResponseException(response.statusCode, message);
} on FormatException catch (_) {
return SyncResponseException(
response.statusCode,
response.reasonPhrase ?? "Request failed",
);
} on Error catch (_) {
return SyncResponseException(
response.statusCode,
response.reasonPhrase ?? "Request failed",
);
return _fromResponseBody(response, body);
} on Exception catch (_) {
// Could be FormatException, or possibly other exceptions.
// Fallback to just using the response header.
return _fromResponseHeader(response);
}
}

static SyncResponseException _fromResponseBody(
http.BaseResponse response, String body) {
final decoded = convert.jsonDecode(body);
final details = _stringOrFirst(decoded['error']?['details']) ?? body;
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
return SyncResponseException(response.statusCode, message);
}

static SyncResponseException _fromResponseHeader(http.BaseResponse response) {
return SyncResponseException(
response.statusCode,
response.reasonPhrase ?? "Request failed",
);
}

int statusCode;
String description;

Expand Down