Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fdc): Handled errors #13433

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to reproduce an error. If a user calls this more than once, the second time would throw an error.

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>()),
);
});
});
}
Loading