Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

209
Visualizações
Observable error handling by Angular's ExceptionHandler

I would like Angular's ExceptionHandler to handle any errors that occur from API calls. The problem is they never reach the ExceptionHandler's handleError method and I'm not sure what I need to do different.

// data.service.ts

addItem(name: string): Observable<any> {

  return this.http.post(name)
           .map(this.extractData)
           .catch((error) => this.handleError(error))
           .share(); 
}

private handleError(error: any): any {
  const msg = `${error.status} - ${error.statusText}`

  // this did not work
  //throw new Error(msg); 

  // also doesn't work
  return Observable.throw(new Error(msg));
}

// item.component.ts

 const observable = this.dataService.addItem('test');
 observable.subscribe(
   (success) => console.log('success'),
   (error) => { throw new Error('test'); // doesn't work }
 );
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Looks like zone is not able to catch the error to pass it to Angular ErrorHandler. May be map will do the trick.

     return this.http.post(name)
       .map(this.extractData)
       .catch((error) => this.handleError(error))
       .map(result=> {
         if(result === 'good one') { return 'good one'; }
         else { new Error(result);}
       })
       .share(); 
over 4 years ago · Santiago Trujillo Relatório

0

Put just like this:

.catch(this.handleError);

Example:

    public fetch(url, env = null): Observable<any> {
        return this.http
            .get(this.buildURL(url, env))
            .map(this.extractData)
            .catch(this.handleError);
    }

    private handleError(error: any) {
        let errMsg = (error.message) ? error.message :
            error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda