[Question] Exception filter object with dependencies
See original GitHub issueI need to create global expection filter object (ApplicationExceptionFilter
) that depends on the Logger
component that is part of the ApplicationModule
Application module:
@Module({
components: [
Logger
]
})
export default class ApplicationModule { }
Logger:
@Component()
export class Logger {
log(message : string) {
// log somewhere
}
}
Expection filter:
@Catch(Error)
export class ApplicationExceptionFilter implements ExceptionFilter {
constructor(private logger : Logger) { }
catch(exception: Error, response: Response) {
this.logger.log('some message')
response.status(500).end()
}
}
Currently I create dependencies manually (I have 2 instances of an logger)
app.useGlobalFilters(new ApplicationExceptionFilter(new Logger()))
Is it possible to retrieve Logger
instance from the ApplicationModule
and pass it to the ApplicationExceptionFilter
?
Or is there a better solution for ExceptionFilter
objects’ dependencies management?
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Error Handling and ExceptionFilter Dependency Injection for ...
In this post I look at how to create an API exception filter to create error object responses, and hook up custom logging...
Read more >Exception Filters MVC - Dependency Injection - Stack Overflow
Try injecting the logger in constructor... public InjectIntoActionInvoker(IUnityContainer container, ILogger lo) { _container = container; ...
Read more >Dependency Injection in action filters in ASP.NET Core
This post looks at a few different techniques for injecting dependencies into action filters in ASP.NET Core. We discuss when each method ...
Read more >Filters in Minimal API apps - Microsoft Learn
Filters that implement the IEndpointFilter interface can resolve dependencies from Dependency Injection(DI), as shown in the previous code.
Read more >Spring MVC Exception Handling - @ControllerAdvice ...
All we need is to annotate these methods with @ExceptionHandler annotation. This annotation takes Exception class as argument. So if we have ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @ddyrcz, @kamilmysliwiec
There is another thing you can do. import
ApplicationExceptionFilter
inApplicationModule
as provider, and export it. It will makeApplicationExceptionFilter
injectable with token.the last thing is to use it in your
main
file:This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.