@@ -3,10 +3,11 @@ import 'dart:convert' as convert;
33import 'dart:io' ;
44
55import 'package:http/http.dart' as http;
6+ import 'package:powersync/src/exceptions.dart' ;
7+ import 'package:powersync/src/log_internal.dart' ;
68
79import 'bucket_storage.dart' ;
810import 'connector.dart' ;
9- import 'log.dart' ;
1011import 'stream_utils.dart' ;
1112import 'sync_status.dart' ;
1213import 'sync_types.dart' ;
@@ -140,7 +141,7 @@ class StreamingSyncImplementation {
140141 }
141142 }
142143 if (response.statusCode != 200 ) {
143- throw _getError (response);
144+ throw SyncResponseException . fromResponse (response);
144145 }
145146
146147 final body = convert.jsonDecode (response.body);
@@ -341,7 +342,7 @@ class StreamingSyncImplementation {
341342 }
342343 }
343344 if (res.statusCode != 200 ) {
344- throw await _getStreamedError (res);
345+ throw await SyncResponseException . fromStreamedResponse (res);
345346 }
346347
347348 // Note: The response stream is automatically closed when this loop errors
@@ -351,49 +352,6 @@ class StreamingSyncImplementation {
351352 }
352353}
353354
354- SyncResponseException _getError (http.Response response) {
355- try {
356- final body = response.body;
357- final decoded = convert.jsonDecode (body);
358- final details = _stringOrFirst (decoded['error' ]? ['details' ]) ?? body;
359- final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
360- return SyncResponseException (response.statusCode, message);
361- } on Error catch (_) {
362- return SyncResponseException (
363- response.statusCode,
364- response.reasonPhrase ?? "Request failed" ,
365- );
366- }
367- }
368-
369- Future <SyncResponseException > _getStreamedError (
370- http.StreamedResponse response) async {
371- try {
372- final body = await response.stream.bytesToString ();
373- final decoded = convert.jsonDecode (body);
374- final details = _stringOrFirst (decoded['error' ]? ['details' ]) ?? body;
375- final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
376- return SyncResponseException (response.statusCode, message);
377- } on Error catch (_) {
378- return SyncResponseException (
379- response.statusCode,
380- response.reasonPhrase ?? "Request failed" ,
381- );
382- }
383- }
384-
385- String ? _stringOrFirst (Object ? details) {
386- if (details == null ) {
387- return null ;
388- } else if (details is String ) {
389- return details;
390- } else if (details is List && details[0 ] is String ) {
391- return details[0 ];
392- } else {
393- return null ;
394- }
395- }
396-
397355/// Attempt to give a basic summary of the error for cases where the full error
398356/// is not logged.
399357String _syncErrorMessage (Object ? error) {
@@ -419,37 +377,3 @@ String _syncErrorMessage(Object? error) {
419377 return '${error .runtimeType }' ;
420378 }
421379}
422-
423- class CredentialsException implements Exception {
424- String message;
425-
426- CredentialsException (this .message);
427-
428- @override
429- toString () {
430- return 'CredentialsException: $message ' ;
431- }
432- }
433-
434- class PowerSyncProtocolException implements Exception {
435- String message;
436-
437- PowerSyncProtocolException (this .message);
438-
439- @override
440- toString () {
441- return 'SyncProtocolException: $message ' ;
442- }
443- }
444-
445- class SyncResponseException implements Exception {
446- int statusCode;
447- String description;
448-
449- SyncResponseException (this .statusCode, this .description);
450-
451- @override
452- toString () {
453- return 'SyncResponseException: $statusCode $description ' ;
454- }
455- }
0 commit comments