#DI in non-global exception filter

15 messages · Page 1 of 1 (latest)

wise fractal
#

I'm adding an exception filter to log errors to some endpoints, so that i dont want/cannot use global filters. the said exception filter will push to a collection in db, so i'd like to leverage a service class for this (with a mongoose model).

all i can find online is to use the APP_FILTER core provider token, but the issue is that it'll make my filter global and i really dont want that.

as a workaround i can create my own decorator which will set a certain metadata and have the filter globally registered to leverage this metadata to act or not, but it is a workaround so i'd like to ask: how to solve my usecase properly?

wise fractal
#

actually this workaround is not even possible bc i cannot get the execution context from an exception filter (to be able to pull its metadata)

#

DI in non-global exception filter

#

i'm looking now that the only way to achieve this is to model an interceptor to do the job of an exception filter, just for having access to execution context. that's one workaround for my workaround... starts to be a lot :/

umbral owl
#

Yes, it's kind of unfortunate, but an interceptor does the job as well

wise fractal
#

actually after trying around, DI looks like working with non-global exception filter if they extend BaseExceptionFilter -> if you pass them as class rather than instance

#

doc says you must not instanciate filters extening BaseExceptionFilter yourself but let the framework do it, so i assumed something else was going on with them.

#

that being said, if you ever use the exception filter outside of its module, you will have to provide its dependencies as global - otherwise it won't be in the module's scope (i think it makes sense, but still worth mentioning)

#

also, it seems to be consistent with every enhancer.

#

this could be a nice addition to the docs website, but i see so many PRs opened already i'm wondering if it's worth it :/

#

FML

umbral owl
#

Yes, it works like that, but you still can't access execution content there, which I thought was your main problem?