Skip to content

Commit f10ff17

Browse files
committed
Fix error message parsing when details is not an array.
1 parent d47673b commit f10ff17

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

packages/powersync/lib/src/streaming_sync.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ HttpException getError(http.Response response) {
353353
try {
354354
final body = response.body;
355355
final decoded = convert.jsonDecode(body);
356-
final details = decoded['error']?['details']?[0] ?? body;
356+
final details = stringOrFirst(decoded['error']?['details']) ?? body;
357357
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
358358
return HttpException(message, uri: response.request?.url);
359359
} on Error catch (_) {
@@ -366,11 +366,23 @@ Future<HttpException> getStreamedError(http.StreamedResponse response) async {
366366
try {
367367
final body = await response.stream.bytesToString();
368368
final decoded = convert.jsonDecode(body);
369-
final details = decoded['error']?['details']?[0] ?? body;
369+
final details = stringOrFirst(decoded['error']?['details']) ?? body;
370370
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
371371
return HttpException(message, uri: response.request?.url);
372372
} on Error catch (_) {
373373
return HttpException(response.reasonPhrase ?? "Request failed",
374374
uri: response.request?.url);
375375
}
376376
}
377+
378+
String? stringOrFirst(Object? details) {
379+
if (details == null) {
380+
return null;
381+
} else if (details is String) {
382+
return details;
383+
} else if (details is List<String>) {
384+
return details[0];
385+
} else {
386+
return null;
387+
}
388+
}

0 commit comments

Comments
 (0)