@@ -61,7 +61,12 @@ class SyncResponseException implements Exception {
6161 static SyncResponseException _fromResponseBody (
6262 http.BaseResponse response, String body) {
6363 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+
6570 final message = '${response .reasonPhrase ?? "Request failed" }: $details ' ;
6671 return SyncResponseException (response.statusCode, message);
6772 }
@@ -73,6 +78,37 @@ class SyncResponseException implements Exception {
7378 );
7479 }
7580
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+
76112 int statusCode;
77113 String description;
78114
@@ -84,18 +120,6 @@ class SyncResponseException implements Exception {
84120 }
85121}
86122
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-
99123class PowersyncNotReadyException implements Exception {
100124 /// @nodoc
101125 PowersyncNotReadyException (this .message);
0 commit comments