diff --git a/src/promise-pool-executor.ts b/src/promise-pool-executor.ts index 4208ed4..15c491a 100644 --- a/src/promise-pool-executor.ts +++ b/src/promise-pool-executor.ts @@ -164,7 +164,7 @@ export class PromisePoolExecutor { }) .catch(error => { this.errors.push( - new PromisePoolError(error.message, item) + new PromisePoolError(this.getErrorMsg(error), item) ) }) @@ -232,4 +232,21 @@ export class PromisePoolExecutor { activeCount (): number { return this.tasks.length } + + /** + * Returns the message of error + * + * @returns {String} + */ + private getErrorMsg (error: any): string { + if (error instanceof Error) { + return error.message + } + + if (typeof error === 'string' || typeof error === 'number') { + return error.toString() + } + + return '' + } } diff --git a/test/promise-pool.js b/test/promise-pool.js index b58b957..b6feeb8 100644 --- a/test/promise-pool.js +++ b/test/promise-pool.js @@ -165,8 +165,24 @@ describe('Promise Pool', () => { // return id // }) -// expect(false).toBe(true) // should not be reached -// } catch (error) { -// const err = new Error('Oh no, not a 3.') -// expect(error).toEqual(err) + // expect(false).toBe(true) // should not be reached + // } catch (error) { + // const err = new Error('Oh no, not a 3.') + // expect(error).toEqual(err) + + it('promise reject with nothing', async () => { + const ids = [1, 2, 3, 4, 5] + + const { errors } = await PromisePool + .withConcurrency(2) + .for(ids) + .process(async id => { + await new Promise((resolve, reject) => setTimeout(reject, 10)) + + return id + }) + + expect(errors.length).toEqual(ids.length) + expect(errors).toSatisfyAll(error => error.message === '') + }) })