首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Github节点操作http错误总是作业失败

Github节点操作http错误总是作业失败
EN

Stack Overflow用户
提问于 2021-01-27 14:52:20
回答 1查看 47关注 0票数 0

我有一个类似如下的github操作:

代码语言:javascript
复制
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处于脱机状态,我的作业将在此操作中失败,并记录:

代码语言:javascript
复制
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运行器隐藏它呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-27 15:12:12

您将需要侦听'error'事件来捕获此事件

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65914172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档