Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Github node action http errors always fails job

I have a github action that looks something like this:

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);
}

if mydomain.com/webhook is offline, my job fails on this action, logging:

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
***

Why does it fail, why doesn't the try catch catch this?

I don't want the entire job to fail if this action fails, so how do I trap this failure and hide it from the GitHub runner?

question from:https://stackoverflow.com/questions/65914172/github-node-action-http-errors-always-fails-job

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will need to listen to the 'error' event to catch this

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...