Skip to content

Commit

Permalink
Merge pull request #1 from iondv/MODWS-35
Browse files Browse the repository at this point in the history
MODWS-35 Перехват HTTP-ошибок в GateWay, фикс цепочки промисов
  • Loading branch information
akumidv authored Apr 30, 2020
2 parents 82d62a4 + 8ef8059 commit 142a714
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions client/GateWay.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const request = require('request');
const sys = require('core/system');
const {readYaml} = require('core/util/read');
const base64 = require('base64-js');
const IonError = require('core/IonError');

/**
* @param {{}} options
Expand Down Expand Up @@ -42,10 +43,11 @@ function GateWay(options) {
Authorization: 'Basic ' + base64.fromByteArray(Buffer.from(clientId + ':' + clientSecret, 'utf8'))
}
}, function (err, response, body) {
if (err) {
if (err || response.statusCode !== 200) {
token = false;
return reject(err);
return reject(err || new IonError(response.statusCode, {}, {message: body}));
}

token = body;
resolve();
});
Expand All @@ -55,12 +57,7 @@ function GateWay(options) {
function byPass(path, method, req, res) {
const destHost = host(req);
ensureToken(destHost)
.catch((err) => {
options.log && options.log.error(`failed to obtain authorization token from ${destHost + options.tokenPath}`);
options.log && options.log.error(err);
res.status(500).send('internal server error');
})
.then(() => {
.then(() => new Promise((resolve, reject) => {
const r = request[method]({
url: destHost + path,
query: req.query,
Expand All @@ -78,7 +75,16 @@ function GateWay(options) {
return;
}
r.pipe(res);
resolve();
});
r.on('error', (err) => {
reject(err);
});
}))
.catch((err) => {
options.log && options.log.error(`failed to obtain authorization token from ${destHost + options.tokenPath}`);
options.log && options.log.error(err);
res.status(500).send('internal server error');
});
}

Expand Down

0 comments on commit 142a714

Please sign in to comment.