node.js - graphql 错误消息获取完整的 http 响应
问题描述
我正在使用apollo-client
. 发生错误时,HTTP 响应如下所示。
{
"data": {
"loginEmail": null
},
"errors": [
{
"message": "VALIDATION_FAILED",
"locations": [
{
"line": 43,
"column": 3
}
],
"path": [
"loginEmail"
],
"errors": [
{
"model": "user",
"field": "password",
"code": "INVALID",
"extra": null
}
]
}
]
}
上面的消息是用 Chrome devtools 捕获的。但是如何捕捉这个完整的响应错误消息apollo-client
呢?
使用以下代码,我得到的只是GraphQL error: VALIDATION_FAILED
async function loginEmail(email, password) {
// ...
try {
const res = await gqclient.mutate({ // gqclient comes from `new ApolloClient({...})`
mutation,
variables: {
"email": email,
"password": password
}
})
return res
} catch (err) {
logger.debug(err) // all I got is `GraphQL error: VALIDATION_FAILED`
return err
}
}
有任何想法吗?
谢谢!
- - - - 更新 - - - -
感谢@cgcgbcbc 的提醒!我的winston
配置错误。
但我不知道为什么?
这没关系(logger.info(err)
可以获得完整的响应消息):
const { createLogger, format, transports } = require('winston')
const logger = createLogger({
level: 'debug',
format: format.combine(
format.timestamp({format: 'YYYY-MM-DDTHH:mm:ss'}),
format.json()
),
transports: [new transports.File({filename: 'logs/new_combined.log'})]
})
// output
// {
// "graphQLErrors":[
// {
// "message":"VALIDATION_FAILED",
// "locations":[
// {
// "line":2,
// "column":3
// }
// ],
// "path":[
// "loginEmail"
// ],
// "errors":[
// {
// "model":"user",
// "field":"password",
// "code":"INVALID",
// "extra":null
// }
// ]
// }
// ],
// "networkError":null,
// "message":"GraphQL error: VALIDATION_FAILED",
// "level":"info",
// "timestamp":"2019-02-26T15:26:19"
// }
这是不行的:
const { createLogger, format, transports } = require('winston')
const logger = createLogger({
level: 'debug',
format: format.combine(
format.timestamp({format: 'YYYY-MM-DDTHH:mm:ss'}),
format.printf(info => {
return JSON.stringify({
timestamp: info.timestamp,
level: info.level,
message: info.message})
})
),
transports: [new transports.File({filename: 'logs/new_combined.log'})]
})
// output
// {
// "timestamp":"2019-02-26T15:36:32",
// "level":"info",
// "message":"GraphQL error: VALIDATION_FAILED"
// }
解决方案
推荐阅读
- api - 权限设置为 isAuthenticated 的路由无法从迅雷客户端(或邮递员)访问
- python - Python PrettyTable 添加 ljust
- r - 数据操作 - 基础
- javascript - 当样式设置为 object-fit:cover 时,从 VIDEO 元素获取画布
- r - R:根据变量应用不同的过滤器
- node.js - 未在 mongodb 指南针中显示数据库
- c++ - 为什么我的代码中没有调用移动构造函数?另外为什么不调用 dtor 只是为了破坏 temp obj?
- memory - 有没有办法强制分配堆栈变量对齐
- python - 如何在 python 程序中进行网页抓取时重定向到不同的网页?
- perl - 由于缺少 perl 模块,在 Windows 10 上使用 LaTeX Workshop 在 VSCode 上格式化文档时出错