@@ -61,7 +61,12 @@ class SyncResponseException implements Exception {
61
61
static SyncResponseException _fromResponseBody (
62
62
http.BaseResponse response, String body) {
63
63
final decoded = convert.jsonDecode (body);
64
- final details = _stringOrFirst (decoded['error' ]? ['details' ]) ?? body;
64
+ final details = switch (decoded['error' ]) {
65
+ final Map <String , Object ?> details => _errorDescription (details),
66
+ _ => null ,
67
+ } ??
68
+ body;
69
+
65
70
final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
66
71
return SyncResponseException (response.statusCode, message);
67
72
}
@@ -73,6 +78,37 @@ class SyncResponseException implements Exception {
73
78
);
74
79
}
75
80
81
+ /// Extracts an error description from an error resonse looking like
82
+ /// `{"code":"PSYNC_S2106","status":401,"description":"Authentication required","name":"AuthorizationError"}` .
83
+ static String ? _errorDescription (Map <String , Object ?> raw) {
84
+ final code = raw['code' ]; // Required, string
85
+ final description = raw['description' ]; // Required, string
86
+
87
+ final name = raw['name' ]; // Optional, string
88
+ final details = raw['details' ]; // Optional, string
89
+
90
+ if (code is ! String || description is ! String ) {
91
+ return null ;
92
+ }
93
+
94
+ final fullDescription = StringBuffer (code);
95
+ if (name is String ) {
96
+ fullDescription.write ('($name )' );
97
+ }
98
+
99
+ fullDescription
100
+ ..write (': ' )
101
+ ..write (description);
102
+
103
+ if (details is String ) {
104
+ fullDescription
105
+ ..write (' ' )
106
+ ..write (details);
107
+ }
108
+
109
+ return fullDescription.toString ();
110
+ }
111
+
76
112
int statusCode;
77
113
String description;
78
114
@@ -84,18 +120,6 @@ class SyncResponseException implements Exception {
84
120
}
85
121
}
86
122
87
- String ? _stringOrFirst (Object ? details) {
88
- if (details == null ) {
89
- return null ;
90
- } else if (details is String ) {
91
- return details;
92
- } else if (details case [final String first, ...]) {
93
- return first;
94
- } else {
95
- return null ;
96
- }
97
- }
98
-
99
123
class PowersyncNotReadyException implements Exception {
100
124
/// @nodoc
101
125
PowersyncNotReadyException (this .message);
0 commit comments