我试图以一种友好的方式显示错误,但是我总是通过我想摆脱的控制台日志获得错误堆栈跟踪。
该想法是使用任何来源(例如Google表格)在我们的平台中创建销售线索。 当线索中提供了无效的电子邮件并将其发布到我们的API时,我将得到想要显示的预期消息,然后显示堆栈跟踪。
我的自定义错误消息是
INVALID FORMAT for email. Object didn't pass validation for format email: as1@mail.
但这就是我得到的:
INVALID FORMAT for email. Object didn't pass validation for format email: as1@mail. What happened: Starting POST request to https://cosmo-charon-production.herokuapp.com/v1/lead/vehicle Received 500 code from https://cosmo-charon-production.herokuapp.com/v1/lead/vehicle?api-key=gIBp04HVdTgsHShJj6bXKwjbcxXTogsh after 62ms Received content "{"code":"SCHEMA_VALIDATION_FAILED","message":"Request validation failed: Parameter (lead) failed sch" INVALID FORMAT for email. Object didn't pass validation for format email: as1@mail. Console logs:
Image showing error displayed in Zapier
我已经在afterResponse
中添加了用于ErrorHandling的中间件,就像Zapier文档中提供的示例之一一样。
函数analyzeAndParse()
从API接收一个错误对象,并返回一个字符串,并以一种友好的方式翻译了错误消息
const checkForErrors = (response, z) => {
// If we get a bad status code, throw an error, using the ErrorTranslator
if (response.status >= 300) {
throw new Error(analyzeAndParse(response.json))
}
// If no errors just return original response
return response
}
这是在我们的平台中创建销售线索并向我们的API发出请求的代码。
function createLead (z, bundle) {
const industry = bundle.inputData.industry
// add product to request based on the inputFields
leadType[industry].addProductFields(bundle.inputData)
const requestOptions = {
url: `${baseUrl}lead/${_.kebabCase(industry)}`,
method: 'POST',
body: JSON.stringify(checkSourceForCreate(bundle.inputData)),
headers: {
'content-type': 'application/json'
}
}
return z.request(requestOptions).then((response) => {
if (response.status >= 300) {
throw new Error(analyzeAndParse(response.content))
}
const content = JSON.parse(response.content)
if (content && content.leads) {
// get only the last lead from the list of leads
content.lead = content.leads[0]
delete content.leads
}
return content
})
}
有什么想法吗?
谢谢!