Petros Kyriakou
Petros Kyriakou

Reputation: 5343

Custom error handler prints console log two/three times

I am making my own exception handler wrapper and i only used a console.log inside just to test that it works. However calling a function that doesn't exist so that i can trigger the error, prints the console.log three times.

However moving the error trigger to ngOnInit console.log two times. Which i find weird?

Note: throw Error('hello') does the same

exception.handler.ts

import { ErrorHandler, Injectable, Injector } from '@angular/core'
import { ExceptionLoggingService } from './exception-logging.service'

@Injectable()
export class ExceptionHandler implements ErrorHandler {
  constructor (private injector: Injector) { }

  handleError (error) {
    // const loggingService = this.injector.get(ExceptionLoggingService)
    // const message = error.message ? error.message : error.toString()

    console.log('hello')

    throw error
  }
}

Using Angular v5.0.3

Upvotes: 1

Views: 63

Answers (1)

br.julien
br.julien

Reputation: 3460

Here is what the Angular documentation says about ErrorHandler:

The default implementation of ErrorHandler prints error messages to the console.

So I think if you remove throw error and simply log your message, it should be print only once in the console:

handleError (error) {
  console.log('hello');
  //throw error;
}

Upvotes: 1

Related Questions