Skip to content

Commit 142a714

Browse files
authored
Merge pull request #1 from iondv/MODWS-35
MODWS-35 Перехват HTTP-ошибок в GateWay, фикс цепочки промисов
2 parents 82d62a4 + 8ef8059 commit 142a714

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

client/GateWay.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const request = require('request');
66
const sys = require('core/system');
77
const {readYaml} = require('core/util/read');
88
const base64 = require('base64-js');
9+
const IonError = require('core/IonError');
910

1011
/**
1112
* @param {{}} options
@@ -42,10 +43,11 @@ function GateWay(options) {
4243
Authorization: 'Basic ' + base64.fromByteArray(Buffer.from(clientId + ':' + clientSecret, 'utf8'))
4344
}
4445
}, function (err, response, body) {
45-
if (err) {
46+
if (err || response.statusCode !== 200) {
4647
token = false;
47-
return reject(err);
48+
return reject(err || new IonError(response.statusCode, {}, {message: body}));
4849
}
50+
4951
token = body;
5052
resolve();
5153
});
@@ -55,12 +57,7 @@ function GateWay(options) {
5557
function byPass(path, method, req, res) {
5658
const destHost = host(req);
5759
ensureToken(destHost)
58-
.catch((err) => {
59-
options.log && options.log.error(`failed to obtain authorization token from ${destHost + options.tokenPath}`);
60-
options.log && options.log.error(err);
61-
res.status(500).send('internal server error');
62-
})
63-
.then(() => {
60+
.then(() => new Promise((resolve, reject) => {
6461
const r = request[method]({
6562
url: destHost + path,
6663
query: req.query,
@@ -78,7 +75,16 @@ function GateWay(options) {
7875
return;
7976
}
8077
r.pipe(res);
78+
resolve();
8179
});
80+
r.on('error', (err) => {
81+
reject(err);
82+
});
83+
}))
84+
.catch((err) => {
85+
options.log && options.log.error(`failed to obtain authorization token from ${destHost + options.tokenPath}`);
86+
options.log && options.log.error(err);
87+
res.status(500).send('internal server error');
8288
});
8389
}
8490

0 commit comments

Comments
 (0)