zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
275 stars 34 forks source link

Compiler swallows Errors thrown in CompilerPasses #208

Open fcaps opened 9 months ago

fcaps commented 9 months ago

Hey,

don't know if this was by mistake, but the Compiler is swallowing all Errors other than RangeError, took me a while to debug this.

  async run () {
    try {
      if (!this._container.frozen) {
        await this._loadExtensions()
        await this._optimize()
        await this._remove()
      }
    } catch (error) {
      if (error instanceof RangeError) {
        throw new ServiceCircularReferenceException()
      }
    }
  }

the original compile from was re-throwing:

  compile () {
    try {
      if (!this.frozen) {
        this._loadExtensions()
        this._optimize()
        this._remove()
      }
    } catch (error) {
      if (error instanceof RangeError) {
        throw new ServiceCircularReferenceException()
      }

      throw error
    }
  }

looking at the code, compiling a frozen container should also thrown an Error imo.

zazoomauro commented 9 months ago

@fcaps Is so strange... all this errors are covered by lot of tests.

fcaps commented 9 months ago

@zazoomauro when I "fix" the issue, some tests fail with a ServiceNotFoundException. was not looking at other tests, but chances are high that they throw a RangeError or something.

image

here a quick test:

//ContainerBuilder.spec.js
    chai.use(require('chai-as-promised'))

    it('should re-throw errors from failing compiler pass', async () => {

        class CompilerPass {
            async process () {
                throw Error('catch me if you can')
            }
        }

        container.addCompilerPass(new CompilerPass())

        await expect(container.compile()).to.be.rejectedWith(Error)
    })
zazoomauro commented 1 month ago

@fcaps feel free to fix it.