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