fix(event-bus-local): Error handling (#3575)

**What**

The error handling was applied on the emit, which always succeed. On the other hand, the `on` which calls the handler, was not handling any errors and therefore crashed the server
This commit is contained in:
Adrien de Peretti
2023-03-24 13:49:29 +00:00
committed by GitHub
parent b57ea22461
commit 4a7bdc917a
4 changed files with 187 additions and 179 deletions
@@ -8,6 +8,7 @@ type InjectedDependencies = {
}
const eventEmitter = new EventEmitter()
eventEmitter.setMaxListeners(Infinity)
// eslint-disable-next-line max-len
export default class LocalEventBusService extends AbstractEventBusModuleService {
@@ -59,18 +60,21 @@ export default class LocalEventBusService extends AbstractEventBusModuleService
continue
}
try {
this.eventEmitter_.emit(event.eventName, event.data)
} catch (error) {
this.logger_.error(
`An error occurred while processing ${event.eventName}: ${error}`
)
}
this.eventEmitter_.emit(event.eventName, event.data)
}
}
subscribe(event: string | symbol, subscriber: Subscriber): this {
this.eventEmitter_.on(event, subscriber)
this.eventEmitter_.on(event, async (...args) => {
try {
// @ts-ignore
await subscriber(...args)
} catch (e) {
this.logger_.error(
`An error occurred while processing ${event.toString()}: ${e}`
)
}
})
return this
}
@@ -2,7 +2,6 @@ import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request"
import { ProductServiceMock } from "../../../../../services/__mocks__/product"
import { ProductVariantServiceMock } from "../../../../../services/__mocks__/product-variant"
import { EventBusServiceMock } from "../../../../../services/__mocks__/event-bus"
describe("POST /admin/products/:id", () => {
describe("successfully updates a product", () => {