|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const BB = require('bluebird') |
4 | 3 | const util = require('util')
|
5 | 4 |
|
6 | 5 | const contentPath = require('./path')
|
@@ -146,19 +145,36 @@ function withContentSri (cache, integrity, fn) {
|
146 | 145 | const cpath = contentPath(cache, digests[0])
|
147 | 146 | return fn(cpath, digests[0])
|
148 | 147 | } else {
|
149 |
| - return BB.any(sri[sri.pickAlgorithm()].map(meta => { |
150 |
| - return withContentSri(cache, meta, fn) |
151 |
| - }, { concurrency: 1 })) |
152 |
| - .catch((err) => { |
153 |
| - if ([].some.call(err, e => e.code === 'ENOENT')) { |
154 |
| - throw Object.assign( |
| 148 | + // Can't use race here because a generic error can happen before a ENOENT error, and can happen before a valid result |
| 149 | + return Promise.all(sri[sri.pickAlgorithm()].map((meta) => { |
| 150 | + return withContentSri(cache, meta, fn).catch((err) => { |
| 151 | + if (err.code === 'ENOENT') { |
| 152 | + return Object.assign( |
155 | 153 | new Error('No matching content found for ' + sri.toString()),
|
156 | 154 | { code: 'ENOENT' }
|
157 | 155 | )
|
158 |
| - } else { |
159 |
| - throw err[0] |
160 | 156 | }
|
| 157 | + return err |
161 | 158 | })
|
| 159 | + })).then((results) => { |
| 160 | + // Return the first non error if it is found |
| 161 | + const result = results.find((r) => !(r instanceof Error)) |
| 162 | + if (result) { |
| 163 | + return result |
| 164 | + } |
| 165 | + |
| 166 | + // Throw the No matching content found error |
| 167 | + const enoentError = results.find((r) => r.code === 'ENOENT') |
| 168 | + if (enoentError) { |
| 169 | + throw enoentError |
| 170 | + } |
| 171 | + |
| 172 | + // Throw generic error |
| 173 | + const genericError = results.find((r) => r instanceof Error) |
| 174 | + if (genericError) { |
| 175 | + throw genericError |
| 176 | + } |
| 177 | + }) |
162 | 178 | }
|
163 | 179 | }
|
164 | 180 |
|
|
0 commit comments