@@ -6,6 +6,7 @@ const request = require('request');
66const sys = require ( 'core/system' ) ;
77const { readYaml} = require ( 'core/util/read' ) ;
88const 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