Skip to content

Commit

Permalink
Merge pull request #19 from powersync-ja/error-messages
Browse files Browse the repository at this point in the history
Improve HTTP error messages
  • Loading branch information
rkistner authored Nov 17, 2023
2 parents 2f57333 + 393fb7d commit e8b606c
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions lib/src/streaming_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class StreamingSyncImplementation {
}
}
if (response.statusCode != 200) {
throw HttpException(response.reasonPhrase ?? "Request failed", uri: uri);
throw getError(response);
}

final body = convert.jsonDecode(response.body);
Expand Down Expand Up @@ -294,8 +294,7 @@ class StreamingSyncImplementation {
}
}
if (res.statusCode != 200) {
throw HttpException(res.reasonPhrase ?? 'Invalid http response',
uri: uri);
throw await getStreamedError(res);
}

// Note: The response stream is automatically closed when this loop errors
Expand All @@ -304,3 +303,29 @@ class StreamingSyncImplementation {
}
}
}

HttpException getError(http.Response response) {
try {
final body = response.body;
final decoded = convert.jsonDecode(body);
final details = decoded['error']?['details']?[0] ?? body;
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
return HttpException(message, uri: response.request?.url);
} on Error catch (_) {
return HttpException(response.reasonPhrase ?? "Request failed",
uri: response.request?.url);
}
}

Future<HttpException> getStreamedError(http.StreamedResponse response) async {
try {
final body = await response.stream.bytesToString();
final decoded = convert.jsonDecode(body);
final details = decoded['error']?['details']?[0] ?? body;
final message = '${response.reasonPhrase ?? "Request failed"}: $details';
return HttpException(message, uri: response.request?.url);
} on Error catch (_) {
return HttpException(response.reasonPhrase ?? "Request failed",
uri: response.request?.url);
}
}

0 comments on commit e8b606c

Please sign in to comment.