我正在使用apollo-client
测试Graphql端点。当发生错误时,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"
// }