Skip to content

Commit

Permalink
fix(fdc): errors are now properly propagated to the user (#13433)
Browse files Browse the repository at this point in the history
* Handled errors

* Only throw errors if errors is not empty
  • Loading branch information
maneesht authored Oct 2, 2024
1 parent 8689a43 commit 973a02f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mutation addDateAndTimestamp($date: Date!, $timestamp: Timestamp!) @auth(level:
mutation seedMovies @auth(level: PUBLIC) {
the_matrix: movie_insert(
data: {
id: "09d5f835656c467787347668bbb44522"
title: "The Matrix"
releaseYear: 1999
genre: "Sci-Fi"
Expand Down Expand Up @@ -70,6 +71,7 @@ mutation thing($title: Any! = "ABC") @auth(level: PUBLIC) {
mutation seedData @auth(level: PUBLIC) {
the_matrix: movie_insert(
data: {
id: "09d5f835656c467787347668bbb44522"
title: "The Matrix"
releaseYear: 1999
genre: "Action"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ class GRPCTransport implements DataConnectTransport {
try {
response = await stub.executeMutation(request,
options: CallOptions(metadata: await getMetadata()));
if (response.errors.isNotEmpty) {
throw Exception(response.errors);
}
return deserializer(jsonEncode(response.data.toProto3Json()));
} on Exception catch (e) {
throw DataConnectError(DataConnectErrorCode.other,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ class RestTransport implements DataConnectTransport {
? DataConnectErrorCode.unauthorized
: DataConnectErrorCode.other,
"Received a status code of ${r.statusCode} with a message '${message}'");
} else {
Map<String, dynamic> bodyJson =
jsonDecode(r.body) as Map<String, dynamic>;
if (bodyJson.containsKey("errors") &&
(bodyJson['errors'] as List).isNotEmpty) {
throw DataConnectError(
DataConnectErrorCode.other, bodyJson['errors'].toString());
}
}

/// The response we get is in the data field of the response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,33 @@ void main() {
body: anyNamed('body'),
)).called(1);
});
test('invokeOperation should throw an error if the server throws one',
() async {
final mockResponse = http.Response("""
{
"data": {},
"errors": [
{
"message": "SQL query error: pq: duplicate key value violates unique constraint movie_pkey",
"locations": [],
"path": [
"the_matrix"
],
"extensions": null
}
]
}""", 200);
when(mockHttpClient.post(any,
headers: anyNamed('headers'), body: anyNamed('body')))
.thenAnswer((_) async => mockResponse);

final deserializer = (String data) => 'Deserialized Data';

expect(
() => transport.invokeOperation(
'testQuery', deserializer, null, null, 'executeQuery'),
throwsA(isA<DataConnectError>()),
);
});
});
}

0 comments on commit 973a02f

Please sign in to comment.