我试图以友好的方式显示错误,但我总是使用我想摆脱的控制台日志获取错误堆栈跟踪。
我们的想法是使用任何来源(例如 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:
我已经为 ErrorHandling 添加了一个中间件afterResponse
,就像 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
})
}
有任何想法吗?
谢谢!