question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Exception filter object with dependencies

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
IsraelZablianovcommented, Dec 20, 2018

Hi @ddyrcz, @kamilmysliwiec

There is another thing you can do. import ApplicationExceptionFilter in ApplicationModule as provider, and export it. It will make ApplicationExceptionFilter injectable with token.

the last thing is to use it in your main file:

...
      app.useGlobalFilters(app.get(ApplicationExceptionFilter))
...
0reactions
lock[bot]commented, Sep 23, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found