Properly handle errors when requesting deltas

When requesting a delta, a `Promise.join` promise chain was producing unhandled
errors since it consisted in a separate promise chain from the parent function which,
was created with `new Promise`. This commit fixes this by creating the new Promise only
when it's needed, avoiding the creation of a separate promise chain.

Closes #432
Change-Type: patch
Signed-off-by: Pablo Carranza Velez <pablo@resin.io>
This commit is contained in:
Pablo Carranza Velez 2017-05-07 11:35:04 -07:00
parent 82dce6186d
commit cb0152c5ea

View File

@ -69,32 +69,29 @@ do ->
return 'resin/scratch' return 'resin/scratch'
findSimilarImage(imgDest) findSimilarImage(imgDest)
.then (imgSrc) -> .then (imgSrc) ->
new Promise (resolve, reject) -> Promise.join docker.getRegistryAndName(imgDest), docker.getRegistryAndName(imgSrc), (dstInfo, srcInfo) ->
Promise.join docker.getRegistryAndName(imgDest), docker.getRegistryAndName(imgSrc), (dstInfo, srcInfo) -> tokenEndpoint = "#{config.apiEndpoint}/auth/v1/token"
tokenEndpoint = "#{config.apiEndpoint}/auth/v1/token" opts =
authOpts = auth:
auth: user: 'd_' + uuid
user: 'd_' + uuid pass: apiKey
pass: apiKey sendImmediately: true
sendImmediately: true json: true
url = "#{tokenEndpoint}?service=#{dstInfo.registry}&scope=repository:#{dstInfo.imageName}:pull&scope=repository:#{srcInfo.imageName}:pull" timeout: requestTimeout
request.getAsync(url, authOpts) url = "#{tokenEndpoint}?service=#{dstInfo.registry}&scope=repository:#{dstInfo.imageName}:pull&scope=repository:#{srcInfo.imageName}:pull"
.spread (res, body) -> request.getAsync(url, opts)
try .get(1)
return JSON.parse(body) .then (b) ->
catch e opts =
return {} timeout: requestTimeout
.then (b) ->
opts =
timeout: requestTimeout
if b?.token?
deltaAuthOpts =
auth:
bearer: b?.token
sendImmediately: true
opts = _.merge(opts, deltaAuthOpts)
if b?.token?
deltaAuthOpts =
auth:
bearer: b?.token
sendImmediately: true
opts = _.merge(opts, deltaAuthOpts)
new Promise (resolve, reject) ->
progress request.get("#{config.deltaHost}/api/v2/delta?src=#{imgSrc}&dest=#{imgDest}", opts) progress request.get("#{config.deltaHost}/api/v2/delta?src=#{imgSrc}&dest=#{imgDest}", opts)
.on 'progress', (progress) -> .on 'progress', (progress) ->
# In request-progress ^2.0.1, "percentage" is a ratio from 0 to 1 # In request-progress ^2.0.1, "percentage" is a ratio from 0 to 1