0

I recently figured out my html content consists of

{{employer.userdata.firstname}}

while there was an implementation change to employer.firstname on class level. The bad side effect was that the wrong binding did not throw any kind of exception and only after some long time of debugging I figured out that employer.userdata == undefined

So I was thinking if there is a chance to implement some generic Exception/ErrorHandler to provide useful information on the console. I tried this approach with

import { ErrorHandler } from '@angular/core';
export class MyErrorHandler extends ErrorHandler {
  constructor() {
    super(true);
  }

  handleError(error) {
    console.log(`MyErrorhandler: ${error}`);
    super.handleError(error);
  }
}

and in my app.module.ts

@NgModule({
  providers: [{ provide: ErrorHandler, useClass: MyErrorHandler }]
})

but still there is no console-log. Any hints how to do it 'properly' ?

EDIT:

From the responses I see that my sample was too simple :-/ I rechecked my code and I have had the code like:

<input class="form-control" type="text" id="firstName"
                                formControlName="firstName"
                                [formControl]="form.controls['firstName']"
                                [(ngModel)]="employer.userdata.firstname">

after changing it to employer.fistname the whole code runs perfectly. I only got an hint after I performed a manual doCheck() which finally throw the Exception. I guess the Exception was thrown already before and somewhere swallowed during the init phase of the complete control ==> how to NOT let is swallow? My approach didn't help :-/

Community
  • 1
  • 1
LeO
  • 4,238
  • 4
  • 48
  • 88
  • 1
    If employer.userdata was undefined, you would get a "Cannot read property firstname of undefined" error in the console, without doing anything special. So the problem is not that. See http://plnkr.co/edit/tEzRrVTeEUWEclKcLWhB?p=preview. – JB Nizet Mar 30 '17 at 06:36
  • The error handler only prevents that your Angular application exits after an error. If `employmer.userdata` actually was `undefined` `{{employer.userdata.firstname}}` should show an error in your browsers console and a custom error handler can prevent showing the error in the console. If there was none, the error handler also won't receive one. – Günter Zöchbauer Mar 30 '17 at 06:37
  • thx for the response - I've updated the example. I've tested Günters comment and see there is an error on the console - but in my use case - in my app none :-/ ==> Can this be turned off? If so -> where? – LeO Mar 30 '17 at 07:08
  • I don't think this can be turned off. – Günter Zöchbauer Mar 30 '17 at 07:14
  • But matter of fact is that if I use `{{employer.userdata.firstname}}` it in the table as second column entry ==> the first row first element is displayed all subsequent elements are not displayed - but the table rows are framed (several 100s)! **AND** not a single error message :-/ – LeO Mar 30 '17 at 07:25

0 Answers0