Commit 74b5be10 Harvey

任务函数包装 异常处理

1 个父辈 0ba2f8af
...@@ -58,6 +58,16 @@ function loadAllJSFiles(dirPaths, options = {}) { ...@@ -58,6 +58,16 @@ function loadAllJSFiles(dirPaths, options = {}) {
}); });
return loadedModules; return loadedModules;
} }
function wrapper_task(func) {
return async function (...args) {
try {
return await func(...args);
}
catch (error) {
return { error: error.message };
}
};
}
(() => { (() => {
const modules = loadAllJSFiles(['./build/job'], { const modules = loadAllJSFiles(['./build/job'], {
ignoreNodeModules: true, ignoreNodeModules: true,
...@@ -72,7 +82,7 @@ function loadAllJSFiles(dirPaths, options = {}) { ...@@ -72,7 +82,7 @@ function loadAllJSFiles(dirPaths, options = {}) {
console.log(key2 + '已存在'); console.log(key2 + '已存在');
continue; continue;
} }
job_handlers.set(key2, m[key2]); job_handlers.set(key2, wrapper_task(m[key2]));
// console.log(key2) // console.log(key2)
} }
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"description": "", "description": "",
"main": "build/index.js", "main": "build/index.js",
"scripts": { "scripts": {
"start": "node ./build/index.js ", "start": "node ./build/index.js",
"watch": "tsc -w", "watch": "tsc -w",
"compile": "tsc" "compile": "tsc"
}, },
......
...@@ -74,6 +74,17 @@ function loadAllJSFiles(dirPaths, options: any = {}) { ...@@ -74,6 +74,17 @@ function loadAllJSFiles(dirPaths, options: any = {}) {
return loadedModules; return loadedModules;
} }
function wrapper_task(func) {
return async function (...args) {
try {
return await func(...args);
} catch (error) {
return { error: error.message };
}
};
}
(() => { (() => {
const modules = loadAllJSFiles(['./build/job'], { const modules = loadAllJSFiles(['./build/job'], {
...@@ -91,7 +102,8 @@ function loadAllJSFiles(dirPaths, options: any = {}) { ...@@ -91,7 +102,8 @@ function loadAllJSFiles(dirPaths, options: any = {}) {
console.log(key2 + '已存在') console.log(key2 + '已存在')
continue; continue;
} }
job_handlers.set(key2, m[key2])
job_handlers.set(key2, wrapper_task(m[key2]))
// console.log(key2) // console.log(key2)
} }
} }
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!