美文网首页
jenkins构建Unhandled promise rejec

jenkins构建Unhandled promise rejec

作者: 施天助 | 来源:发表于2019-01-15 23:11 被阅读0次

jenkins构建node脚本的时候,如果你的主程序最后是个返回promise的话就不能中断构建脚本,但有时我们需要在node出错的时候就终止运行,并让jenkins构建状态变成FAILURE。比如:
构建脚本:

node a.js
echo 构建程序后面的步骤

想要在a.js 出错时就终止,不要再执行到echo 构建程序后面的步骤。

a.js 内容:

(async function main(){
    // try{
        await new Promise((resolve, reject)=>{
            setTimeout(()=>{
                console.log('timeout');
                reject('reject')
            }, 1000);
        })
        console.log('after await')
    // }catch(e){
    //     console.log('try catch');
    //     throw new Error(e);
    // }
    // console.log('after try')
})

就算你打开try catch,jenkins也是只能看到错误:UnhandledPromiseRejectionWarning: Unhandled promise rejection,而jenkins状态却仍然是success的,不会变成 Finished: FAILURE。

方法:利用 process.exit(1);

(async function main(){
        // try{
            await new Promise((resolve, reject)=>{
                setTimeout(()=>{
                    console.log('timeout');
                    reject('reject')
                }, 1000);
            })
            console.log('after await')
        // }catch(e){
        //     console.log('try catch');
        //     throw new Error(e);
        // }
        // console.log('after try')
    })().catch(function(e){
        process.exit(1);
        // throw new Error(e);
    });

相关文章

网友评论

      本文标题:jenkins构建Unhandled promise rejec

      本文链接:https://www.haomeiwen.com/subject/jfoudqtx.html