下午好,
我正在尝试对我所有路线上的const { handleRouteError } = require('../handleRouteError');
class Controller {
async tryFunction(promise, onSuccess, onFail) {
try {
const data = await promise;
onSuccess(data);
} catch (error) {
handleRouteError(error);
onFail(error);
}
}
}
module.exports = Controller;
街区进行标准化。
const { getDays } = require('./controllers/getDays');
const controller = new Controller;
router.get('/days', async function getDayDays(req, res, next) {
await controller.tryFunction(
getDays(res.locals.user_id),
(data) => res.json(data),
(err) => next(err)
);
});
onSuccess
onFail
和public class MyTestSuite extends JavaDataFrameSuiteBase implements Serializable {
....
@Test
public void oneTest() {
ClassLoader classLoader = getClass().getClassLoader();
File testData = new File(classLoader.getResource("testFile1.txt").getFile());
JavaRDD<String> lines = jsc().textFile(testData.getAbsolutePath());
...
函数是不好的做法吗?欢迎所有意见!答案 0 :(得分:1)
我会尽量坚持内置的错误处理方法,而不要发明自己的错误处理方法。
如果您打算将错误传递给handleRouterError
,然后再将错误传递给调用方,则可以执行以下操作:
class Controller {
async resolve(promise) {
try {
return await promise;
} catch (error) {
handleRouteError(error);
throw error;
}
}
}
并像这样使用resolve
方法:
const { getDays } = require('./controllers/getDays');
const controller = new Controller;
router.get('/days', async function getDayDays(req, res, next) {
try {
res.json(await controller.resolve(getDays(res.locals.user_id)));
} catch (err) {
next(err)
}
});