Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: safely get error message when catch error #19

Merged
merged 1 commit into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/promise-pool-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class PromisePoolExecutor<T> {
})
.catch(error => {
this.errors.push(
new PromisePoolError(error.message, item)
new PromisePoolError(this.getErrorMsg(error), item)
)
})

Expand Down Expand Up @@ -232,4 +232,21 @@ export class PromisePoolExecutor<T> {
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 ''
}
}
24 changes: 20 additions & 4 deletions test/promise-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '')
})
})