From ab6b1e2c327e7d115ea1384a7b0f711ef909ee1b Mon Sep 17 00:00:00 2001 From: Julien Fontanet Date: Mon, 15 Aug 2016 16:33:07 +0200 Subject: [PATCH] chore(Xapi#_transportCall): simplify and improve code It may even fixes some issues. --- packages/xen-api/src/index.js | 51 ++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/xen-api/src/index.js b/packages/xen-api/src/index.js index a5ea97f3f..7a965943b 100644 --- a/packages/xen-api/src/index.js +++ b/packages/xen-api/src/index.js @@ -418,8 +418,9 @@ export class Xapi extends EventEmitter { } // Low level call: handle transport errors. - _transportCall (method, args, startTime = Date.now(), tries = 1) { - return this._rawCall(method, args) + _transportCall (method, args) { + let tries = 1 + const loop = () => this._rawCall(method, args) ::pCatch(isNetworkError, isXapiNetworkError, error => { debug('%s: network error %s', this._humanId, error.code) @@ -447,28 +448,30 @@ export class Xapi extends EventEmitter { return this._transportCall(method, args, startTime) }) - .then( - result => { - debug( - '%s: %s(...) [%s] ==> %s', - this._humanId, - method, - ms(Date.now() - startTime), - kindOf(result) - ) - return result - }, - error => { - debug( - '%s: %s(...) [%s] =!> %s', - this._humanId, - method, - ms(Date.now() - startTime), - error - ) - throw error - } - ) + + const startTime = Date.now() + return loop().then( + result => { + debug( + '%s: %s(...) [%s] ==> %s', + this._humanId, + method, + ms(Date.now() - startTime), + kindOf(result) + ) + return result + }, + error => { + debug( + '%s: %s(...) [%s] =!> %s', + this._humanId, + method, + ms(Date.now() - startTime), + error + ) + throw error + } + ) } // Lowest level call: do not handle any errors.