是否可以在 NodeJs 中使用异常中断 Javascript 代码流?
Is it possible to interrupt Javascript the flow of code with an Exception in NodeJs?
在 Java 中,我可以做这样的事情,我想知道在 NodeJS 中是否可以做这样的事情?
function is_document_redundant(document) {
// pseudo code, not real
result = mongoose.Model.find(document.id);
if (result != null) {
throw new RedundantDocumentException(document);
}
}
function save_document(document) {
// pseudo code, not real
try {
is_document_redundant(document);
mongoose.Model.save(document);
} catch(err) {
console.log(err)
}
}
明确一点,如果这一行抛出异常:
is_document_redundant(document);
那么这条线永远不会发生:
mongoose.Model.save(document);
也就是抛出Exception后脚本基本结束
我尝试编写这段代码,但不幸的是,脚本似乎在异常之后继续。即使代码是多余的,这也会引发异常:
is_document_redundant(document);
脚本仍然执行这一行:
mongoose.Model.save(document);
我在数据库中堆积了多余的文档。我想知道如何避免这种情况,而不必到处都有一堆丑陋的 catch() 块。我宁愿让异常冒泡到顶部并在一个 catch 块中处理它们,但我需要这些异常来中断代码并将控制流直接带到顶层 catch() 语句。
编辑:
好的,现在我正在尝试使用 await
但是:
我有一条这样开始的 Express 路线:
app.post('/ams', async (req, res) => {
try {
然后尝试:
let is_redundant = await document_is_redundant(AMS_945, unique_id);
console.log(is_redundant);
if (is_redundant) {
throw new DocumentIsRedundantException(unique_id);
}
但我得到:
SyntaxError: await is only valid in async function
async function is_document_redundant(document) {
// pseudo code, not real
result = await mongoose.Model.find(document.id);
return result != null
}
async function save_document(document) {
// pseudo code, not real
cosnt isRedundant = await is_document_redundant(document)
if (isRedundant) {
mongoose.Model.save(document);
return res.send('success')
} else {
return res.status(404).('refused to save')
}
}
如果只是想要不同的结果,不一定需要依赖error。
在客户端,您可以使用res.ok
来检查您收到的响应是否不是 200 状态码。 (客户端的 try-catch 不起作用,因为手工错误不是网络错误)
在 Java 中,我可以做这样的事情,我想知道在 NodeJS 中是否可以做这样的事情?
function is_document_redundant(document) {
// pseudo code, not real
result = mongoose.Model.find(document.id);
if (result != null) {
throw new RedundantDocumentException(document);
}
}
function save_document(document) {
// pseudo code, not real
try {
is_document_redundant(document);
mongoose.Model.save(document);
} catch(err) {
console.log(err)
}
}
明确一点,如果这一行抛出异常:
is_document_redundant(document);
那么这条线永远不会发生:
mongoose.Model.save(document);
也就是抛出Exception后脚本基本结束
我尝试编写这段代码,但不幸的是,脚本似乎在异常之后继续。即使代码是多余的,这也会引发异常:
is_document_redundant(document);
脚本仍然执行这一行:
mongoose.Model.save(document);
我在数据库中堆积了多余的文档。我想知道如何避免这种情况,而不必到处都有一堆丑陋的 catch() 块。我宁愿让异常冒泡到顶部并在一个 catch 块中处理它们,但我需要这些异常来中断代码并将控制流直接带到顶层 catch() 语句。
编辑:
好的,现在我正在尝试使用 await
但是:
我有一条这样开始的 Express 路线:
app.post('/ams', async (req, res) => {
try {
然后尝试:
let is_redundant = await document_is_redundant(AMS_945, unique_id);
console.log(is_redundant);
if (is_redundant) {
throw new DocumentIsRedundantException(unique_id);
}
但我得到:
SyntaxError: await is only valid in async function
async function is_document_redundant(document) {
// pseudo code, not real
result = await mongoose.Model.find(document.id);
return result != null
}
async function save_document(document) {
// pseudo code, not real
cosnt isRedundant = await is_document_redundant(document)
if (isRedundant) {
mongoose.Model.save(document);
return res.send('success')
} else {
return res.status(404).('refused to save')
}
}
如果只是想要不同的结果,不一定需要依赖error。
在客户端,您可以使用res.ok
来检查您收到的响应是否不是 200 状态码。 (客户端的 try-catch 不起作用,因为手工错误不是网络错误)