github-actions - Github 节点操作 http 错误总是导致作业失败
问题描述
我有一个看起来像这样的 github 操作:
const http = require('http');
const core = require('@actions/core');
const options = {
hostname: 'mydomain.com',
port: 80,
path: '/webhook',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
};
const postData = {
foo: 'bar'
}
try {
const strPostData = querystring.stringify(postData);
options.headers['Content-Length'] = Buffer.byteLength(strPostData);
const req = http.request(options)
req.write(strPostData)
req.end()
} catch (error) {
core.info(error.message);
}
如果mydomain.com/webhook
处于脱机状态,我的工作将在此操作上失败,记录:
events.js:187
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED xxx.xxx.xxx.xxx:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14)
Emitted 'error' event on ClientRequest instance at:
at Socket.socketErrorListener (_http_client.js:406:9)
at Socket.emit (events.js:210:5)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:80:21) ***
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: 'xxx.xxx.xxx.xxx',
port: 80
***
为什么它失败了,为什么没有try catch
抓住这个?
如果此操作失败,我不希望整个作业失败,那么如何捕获此失败并将其隐藏在 GitHub 运行器中?
解决方案
您将需要收听该'error'
事件才能捕捉到这个
const strPostData = querystring.stringify(postData);
options.headers['Content-Length'] = Buffer.byteLength(strPostData);
const req = http.request(options)
req.on('error', (err) => {/*Error Caught*/})
req.write(strPostData)
req.end()
推荐阅读
- c++ - 相关 C++ 类中多态性的最佳实践?
- c# - 我可以将 Visual Studio 配置为允许调试步骤进入 NuGet 包吗?
- typescript - 如何为具有子级和父级的实体创建域模型
- java - 从 Excel 中读取表格数据并显示 Java 中唯一组合的列数
- python - 在 csv 文件中查找字符串时提取一行
- c++ - 如果 T 不是整数,如何禁用非空 std::set 作为函数参数?
- java - 仅在特定片段上的 Android 导航组件侧导航
- java - HttpUrlConnect 不作为线程的一部分发送发布请求
- javascript - 我可以在变量上引用点击事件吗?
- google-apps-script - 如何在 GAS 中使用 Urlfetch 找出要复制的 Web 表的正确链接