[misc] RoomManager: emitOnCompletion: properly handle Promise rejections

```
result = Promise.all([<Promise that rejects eventually>]) # rejection 1
result.then () -> RoomEvents.emit(eventName)              # rejection 2
result.catch (err) -> RoomEvents.emit(eventName, err)     # handle r1
```
As shown above, the second rejection remains unhandled. The fix is to
 chain the `.catch()` onto the `.then()` Promise.
This commit is contained in:
Jakob Ackermann 2020-06-17 09:29:12 +01:00
parent 06a8dc8ec1
commit 5f7841526f
2 changed files with 27 additions and 3 deletions

View file

@ -39,9 +39,9 @@ module.exports = RoomManager =
@leaveEntity client, entity, id
emitOnCompletion: (promiseList, eventName) ->
result = Promise.all(promiseList)
result.then () -> RoomEvents.emit(eventName)
result.catch (err) -> RoomEvents.emit(eventName, err)
Promise.all(promiseList)
.then(() -> RoomEvents.emit(eventName))
.catch((err) -> RoomEvents.emit(eventName, err))
eventSource: () ->
return RoomEvents

View file

@ -1,4 +1,5 @@
chai = require('chai')
expect = chai.expect
should = chai.should()
sinon = require("sinon")
modulePath = "../../../app/js/RoomManager.js"
@ -20,6 +21,29 @@ describe 'RoomManager', ->
sinon.spy(@RoomEvents, 'emit')
sinon.spy(@RoomEvents, 'once')
describe "emitOnCompletion", ->
describe "when a subscribe errors", ->
afterEach () ->
process.removeListener("unhandledRejection", @onUnhandled)
beforeEach (done) ->
@onUnhandled = (error) =>
@unhandledError = error
done(new Error("unhandledRejection: #{error.message}"))
process.on("unhandledRejection", @onUnhandled)
reject = undefined
subscribePromise = new Promise((_, r) -> reject = r)
promises = [subscribePromise]
eventName = "project-subscribed-123"
@RoomEvents.once eventName, () ->
setTimeout(done, 100)
@RoomManager.emitOnCompletion(promises, eventName)
setTimeout(() -> reject(new Error("subscribe failed")))
it "should keep going", () ->
expect(@unhandledError).to.not.exist
describe "joinProject", ->
describe "when the project room is empty", ->