如何解决节点Sqlite3错误
我在node中使用sqlite3包,并开始刷新我的REST API。我决定围绕DB调用并在控制器中创建一个Promise包装器,使用async / await调用这些函数并将返回值设置为变量。然后,检查变量并设置响应对象。对于成功的案例,它运作良好,找到了解决办法,并做出了回应。问题是,在sqlite3中,我很难检查错误。我对DB服务中的undefined进行了基本检查,如果遇到错误,它确实会引发错误,但是该错误会立即转到控制器的CATCH部分,并且不允许我将其包装在我想要的API响应中喜欢定义。这些是单独的文件(控制器与服务)。在sqlite3的错误检查方面,我找不到很多有用的信息,因为它稀疏。理想情况下,服务会引发错误,然后我可以将其包装到标准化响应对象中并发送。
------位置
const getById = async (req,res,next) => {
response = {};
try {
let location = await getLocationById(req.params.id); // <-- IF NO VALUE FOUND,location IS nothing
if (error) { // <----- THIS IS WHERE I'D LIKE TO CHECK FOR ERRORS.
response.status = 404;
response.message = 'Error attempting to get location';
} else {
response.status = 200;
response.message = 'Success';
response.data = location;
}
res.json(response);
} catch (error) {
res.json(response);
}
};
------------服务
const getLocationById = (id) => {
return new Promise((resolve,reject) => {
let sql = 'SELECT * FROM Locations WHERE id = ?;';
db.get(sql,[id],(error,location) => {
if (location !== undefined) {
resolve(location);
} else {
reject(new Error('Error attempting to get location by id'));
}
});
});
};
解决方法
您只需要将对getLocationById()
的调用包装到另一个try/catch
中。从那里,您可以决定要包装还是引发错误。如果没有要添加的其他代码可能会引发错误,则可以删除外部的try/catch
。内嵌评论中的更多解释和建议:
const getLocationById = (id) => {
return new Promise((resolve,reject) => {
const sql = "SELECT * FROM Locations WHERE id = ?;";
db.get(sql,[id],(error,location) => {
// check for an error from the db here
if (error) return reject(error);
// check for an empty result here and throw your own error
if (!location) {
// set a property so we can tell that it wasn't a db error just not found
const myError = new Error("Error attempting to get location by id");
myError.notFound = true;
return reject(myError);
}
// otherwise resolve the result
return resolve(location);
});
});
};
const getById = async (req,res,next) => {
// use const since it is not reassigned,only properties are changed
const response = {};
try {
// code here that might throw an exception
try {
// use const if you aren't going to reassign `location`
const location = await getLocationById(req.params.id);
// we were able to load the location if we get here
response.status = 200;
response.message = "Success";
response.data = location;
} catch (error) {
// maybe check to see if we want to wrap or raise the error
// maybe call console.log(error) to see the contents
const doWeWantToWrapTheError = somethingToCheckTypeEtc(error);
if (doWeWantToWrapTheError) {
if (error.notFound) {
// no db errors,just not found
response.status = 404;
} else {
// some kind of db error so set to "internal server error"
response.status = 500;
}
response.message = "Error attempting to get location";
} else {
// raise the error to the outer try/catch
throw error;
}
}
// code here that might throw an exception
return res.json(response);
} catch (unexpected) {
// some other error occurred that was not caught above (unlikely)
// maybe call console.log(unexpected) to see the contents
response.error = unexpected.message;
return res.json(response);
}
};
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。