Skip to content

Commit

Permalink
feat: handle ENETUNREACH error on httpclient (#4792)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Nov 10, 2021
1 parent 192d42b commit 189c478
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -13,7 +13,7 @@ jobs:
fail-fast: false
matrix:
os: [ ubuntu-latest, macOS-latest, windows-latest ]
node-version: [ 8, 10, 12, 14 ]
node-version: [ 8, 10, 12, 14, 16 ]
steps:
- name: Checkout Git Source
uses: actions/checkout@master
Expand Down
25 changes: 0 additions & 25 deletions .travis.yml

This file was deleted.

6 changes: 6 additions & 0 deletions docs/source/zh-cn/faq/httpclient_ENETUNREACH.md
@@ -0,0 +1,6 @@
# connect ENETUNREACH 异常

ENETUNREACH 的全称是 Network unreachable,网络不可达。
一般是请求的域名解析出来的网络地址不可用,需要检查你的运行环境网络是否可用。
如果在企业内部,很有可能受网络限制不允许对外网络互联,可以联系对应的网络工程师排查。

15 changes: 14 additions & 1 deletion lib/core/httpclient.js
Expand Up @@ -4,6 +4,13 @@ const Agent = require('agentkeepalive');
const HttpsAgent = require('agentkeepalive').HttpsAgent;
const urllib = require('urllib');
const ms = require('humanize-ms');
const { FrameworkBaseError } = require('egg-errors');

class HttpClientError extends FrameworkBaseError {
get module() {
return 'httpclient';
}
}

class HttpClient extends urllib.HttpClient2 {
constructor(app) {
Expand Down Expand Up @@ -42,7 +49,13 @@ class HttpClient extends urllib.HttpClient2 {
}

// the Promise style
return super.request(url, args);
return super.request(url, args)
.catch(err => {
if (err.code === 'ENETUNREACH') {
throw HttpClientError.create(err.message, err.code);
}
throw err;
});
}

curl(url, args, callback) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -27,6 +27,7 @@
"egg-cookies": "^2.3.0",
"egg-core": "^4.18.0",
"egg-development": "^2.4.2",
"egg-errors": "^2.3.0",
"egg-i18n": "^2.0.0",
"egg-jsonp": "^2.0.0",
"egg-logger": "^2.3.2",
Expand Down
26 changes: 26 additions & 0 deletions test/lib/core/httpclient.test.js
Expand Up @@ -2,6 +2,7 @@

const assert = require('assert');
const mm = require('egg-mock');
const urllib = require('urllib');
const Httpclient = require('../../../lib/core/httpclient');
const utils = require('../../utils');

Expand Down Expand Up @@ -87,6 +88,31 @@ describe('test/lib/core/httpclient.test.js', () => {
await client.requestThunk(url, args);
});

it('should mock ENETUNREACH error', async () => {
mm(urllib.HttpClient2.prototype, 'request', () => {
const err = new Error('connect ENETUNREACH 1.1.1.1:80 - Local (127.0.0.1)');
err.code = 'ENETUNREACH';
return Promise.reject(err);
});
await assert.rejects(async () => {
await client.request(url);
}, err => {
assert(err.name === 'HttpClientError');
assert(err.code === 'httpclient_ENETUNREACH');
assert(err.message === 'connect ENETUNREACH 1.1.1.1:80 - Local (127.0.0.1) [https://eggjs.org/zh-cn/faq/httpclient_ENETUNREACH]');
return true;
});
});

it('should handle timeout error', async () => {
await assert.rejects(async () => {
await client.request(url + '/timeout', { timeout: 100 });
}, err => {
assert(err.name === 'ResponseTimeoutError');
return true;
});
});

describe('httpclient.httpAgent.timeout < 30000', () => {
let app;
before(() => {
Expand Down

0 comments on commit 189c478

Please sign in to comment.