当没有 ID 匹配时,如何显示我的 catch 块消息?
How do I display my catch block message when there is no ID match?
我正在从 API 获取数据并将其显示在我的本地服务器上。
下面是我的代码,用于获取与 API 数据中的 ID 匹配的数据:
router.get('/:id', async (req, res) => {
checkString(req.params.id)
try {
const person = await peopleData.getPersonById(req.params.id);
res.json(person);
} catch (e) {
res.status(404).json({ message: 'There is no person with that ID' });
}
如果没有匹配项,我想像在 catch 块中那样显示消息,但代码不会出现在那里,因为从技术上讲,没有匹配项不是错误。
所以我尝试了以下代码来获取此消息:
router.get('/:id', async (req, res) => {
checkString(req.params.id)
try {
const person = await peopleData.getPersonById(req.params.id);
if(!person) res.json('There is no person with that ID'); // Added new line here
res.json(person);
} catch (e) {
res.status(404).json({ message: 'There is no person with that ID' });
}
这是可行的,但它会打印带有引号的消息作为字符串,如果没有找到匹配项,有没有办法在 catch 块中显示消息?
你可以抛出一个错误,catch 会显示它。
if(!person) throw new Error("There is no person with that ID");
....
然后在抓...
catch(e){
res.status(404).json({ message: e.message })
}
如果您要将人们发送到全屏“错误堆栈”页面,那么您可能不需要使用 res.json()!您也可以使用 res.send()
if(!person){ res.send('<p>There is no person with that ID</p>'; return; }
// Or
if(!person){ res.send('There is no person with that ID'; return; }
您正在 returning Json 响应,因此看起来您的消费者不是网页而是另一个应用程序。如果是这样,你应该 return undefined
或 null
如果没有找到人,让网页或消费者决定显示什么信息。原因是:
- 修改网页应该比代码更容易,通常 UI 或营销人员总是希望微调(通常多次)网页上的每条消息。
- 您的应用是 API 应用。显示用户未找到消息的位置可能在很多步之外。或者显示消息可能 不合适 ,例如,如果找不到用户,消费应用程序可能希望重定向 to/show 注册页面。
- 您的网站可能是多语言的,您不希望后端参与其中。
“找不到用户”在很多情况下并不是真正的错误,但这完全取决于您的应用程序。
您的情况下的 catch 块应该用于处理其他错误,例如,您的数据库服务器可能已关闭,或者数据库请求可能已超时等。您当前的代码将如果存在数据库错误,则误导性地显示“找不到用户”!
我也会让 Express 错误处理程序处理此类实际错误,而不是为您拥有的每个 API 函数编写错误处理代码:
router.get('/:id', async (req, res, next) => {
checkString(req.params.id);
try {
const person = await peopleData.getPersonById(req.params.id);
res.json(person); // assuming getPersonById returns null if user not found
} catch (e) {
next(e);
});
您的 Express 错误处理程序,其中调用上述 next
函数,应该是这样的(假设 router
是您的 Express 应用程序):
router.use((err, req, res, next) => {
let statusCode = err.status || 500;
// Assuming your app need to return only json responses
res.json(err);
});
我正在从 API 获取数据并将其显示在我的本地服务器上。
下面是我的代码,用于获取与 API 数据中的 ID 匹配的数据:
router.get('/:id', async (req, res) => {
checkString(req.params.id)
try {
const person = await peopleData.getPersonById(req.params.id);
res.json(person);
} catch (e) {
res.status(404).json({ message: 'There is no person with that ID' });
}
如果没有匹配项,我想像在 catch 块中那样显示消息,但代码不会出现在那里,因为从技术上讲,没有匹配项不是错误。
所以我尝试了以下代码来获取此消息:
router.get('/:id', async (req, res) => {
checkString(req.params.id)
try {
const person = await peopleData.getPersonById(req.params.id);
if(!person) res.json('There is no person with that ID'); // Added new line here
res.json(person);
} catch (e) {
res.status(404).json({ message: 'There is no person with that ID' });
}
这是可行的,但它会打印带有引号的消息作为字符串,如果没有找到匹配项,有没有办法在 catch 块中显示消息?
你可以抛出一个错误,catch 会显示它。
if(!person) throw new Error("There is no person with that ID");
....
然后在抓...
catch(e){
res.status(404).json({ message: e.message })
}
如果您要将人们发送到全屏“错误堆栈”页面,那么您可能不需要使用 res.json()!您也可以使用 res.send()
if(!person){ res.send('<p>There is no person with that ID</p>'; return; }
// Or
if(!person){ res.send('There is no person with that ID'; return; }
您正在 returning Json 响应,因此看起来您的消费者不是网页而是另一个应用程序。如果是这样,你应该 return undefined
或 null
如果没有找到人,让网页或消费者决定显示什么信息。原因是:
- 修改网页应该比代码更容易,通常 UI 或营销人员总是希望微调(通常多次)网页上的每条消息。
- 您的应用是 API 应用。显示用户未找到消息的位置可能在很多步之外。或者显示消息可能 不合适 ,例如,如果找不到用户,消费应用程序可能希望重定向 to/show 注册页面。
- 您的网站可能是多语言的,您不希望后端参与其中。
“找不到用户”在很多情况下并不是真正的错误,但这完全取决于您的应用程序。
您的情况下的 catch 块应该用于处理其他错误,例如,您的数据库服务器可能已关闭,或者数据库请求可能已超时等。您当前的代码将如果存在数据库错误,则误导性地显示“找不到用户”!
我也会让 Express 错误处理程序处理此类实际错误,而不是为您拥有的每个 API 函数编写错误处理代码:
router.get('/:id', async (req, res, next) => {
checkString(req.params.id);
try {
const person = await peopleData.getPersonById(req.params.id);
res.json(person); // assuming getPersonById returns null if user not found
} catch (e) {
next(e);
});
您的 Express 错误处理程序,其中调用上述 next
函数,应该是这样的(假设 router
是您的 Express 应用程序):
router.use((err, req, res, next) => {
let statusCode = err.status || 500;
// Assuming your app need to return only json responses
res.json(err);
});