[英]Unable to retrieve errors occured in Graphql mutation in flutter project
我在我的 flutter 应用程序中使用 package graphql_flutter进行 GraphQL 操作。 查询和突变进展顺利,但我无法按照他们文档中提到的方式检索错误。 每次我收到一般错误消息时,
ClientException: Failed to connect to http://127.0.0.1:3006/graphql:
我做得到,
print(result.exception.toString());
我的突变看起来像,
final MutationOptions mutationOptions = MutationOptions(
documentNode: gql(mutationString),
variables: vars
);
final QueryResult result = await _instance._client.mutate(mutationOptions);
if (result.hasException) {
// none of the following prints the expected error.
print(result.exception.clientException.message);
print(result.exception.graphqlErrors);
print(result.exception.toString());
}
print(result.data);
return result.data;
而在阿波罗客户端,我的错误是:
{
"errors": [
{
"message": "Invalid Phone number provided",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"otp"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
....
但我什么都没有。
注意:成功响应如期而至。 我想知道如何获得 graphql 错误。
我发现了问题。 这是因为 android 无法从模拟器连接到127.0.0.1
或localhost
。 我用我的本地 IP 地址替换了主机,现在它工作正常。
把它放在一个 try/catch 块中,看看它是否可以捕获任何异常
final QueryResult result = await _instance._client.mutate(mutationOptions);
最初的问题是如何获得 graphql 错误。 我正在使用 graphql: ^5.0.0
我检查了文档并找到了这个例子:
if (result.hasException) {
if (result.exception.linkException is NetworkException) {
// handle network issues, maybe
}
return Text(result.exception.toString())
}
但这只是给了我异常,而不是错误,我将结果异常转换为错误类型并能够得到消息:
if (result.hasException) {
if (result.exception!.linkException is ServerException) {
ServerException exception =
result.exception!.linkException as ServerException;
var errorMessage = exception.parsedResponse!.errors![0].message;
print(errorMessage);
throw Exception(errorMessage);
}
}
对于一个简单的消息来说,这似乎需要做很多工作,我想知道是否还有另一种更简单的内置方法可以做到这一点
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.