|
| 1 | +import 'dart:async'; |
| 2 | +import 'dart:convert' as convert; |
| 3 | + |
| 4 | +import 'package:http/http.dart' as http; |
| 5 | + |
| 6 | +/// This indicates an error with configured credentials. |
| 7 | +class CredentialsException implements Exception { |
| 8 | + String message; |
| 9 | + |
| 10 | + CredentialsException(this.message); |
| 11 | + |
| 12 | + @override |
| 13 | + toString() { |
| 14 | + return 'CredentialsException: $message'; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/// An internal protocol exception. |
| 19 | +/// |
| 20 | +/// This indicates that the server sent an invalid response. |
| 21 | +class PowerSyncProtocolException implements Exception { |
| 22 | + String message; |
| 23 | + |
| 24 | + PowerSyncProtocolException(this.message); |
| 25 | + |
| 26 | + @override |
| 27 | + toString() { |
| 28 | + return 'SyncProtocolException: $message'; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// An error that received from the sync service. |
| 33 | +/// |
| 34 | +/// Examples include authorization errors (401) and temporary service issues (503). |
| 35 | +class SyncResponseException implements Exception { |
| 36 | + /// Parse an error response from the PowerSync service |
| 37 | + static Future<SyncResponseException> fromStreamedResponse( |
| 38 | + http.StreamedResponse response) async { |
| 39 | + try { |
| 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 | + ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Parse an error response from the PowerSync service |
| 54 | + static SyncResponseException fromResponse(http.Response response) { |
| 55 | + try { |
| 56 | + 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 Error catch (_) { |
| 62 | + return SyncResponseException( |
| 63 | + response.statusCode, |
| 64 | + response.reasonPhrase ?? "Request failed", |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + int statusCode; |
| 70 | + String description; |
| 71 | + |
| 72 | + SyncResponseException(this.statusCode, this.description); |
| 73 | + |
| 74 | + @override |
| 75 | + toString() { |
| 76 | + return 'SyncResponseException: $statusCode $description'; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +String? _stringOrFirst(Object? details) { |
| 81 | + if (details == null) { |
| 82 | + return null; |
| 83 | + } else if (details is String) { |
| 84 | + return details; |
| 85 | + } else if (details is List && details[0] is String) { |
| 86 | + return details[0]; |
| 87 | + } else { |
| 88 | + return null; |
| 89 | + } |
| 90 | +} |
0 commit comments