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

249
Visualizações
Angular method decorator error handling with subscriptions

Currently, I have an angular application with a method decorator to handle errors of all the methods in the component. It catches all the errors from methods, But it is unable to catch the error inside subscribe. Any suggestions to do this?

This is my current code.

This is the sampling method I want to catch the errors

  @logActionErrors()
  getEmailSettings() {
      this.sharedService.getSMTPConfigurations().subscribe(() => {
        throw('this is an error');
      }, (ex) =>{
        console.log(ex);
      })
  }

This is my Method Decorator

export function logActionErrors(): any {
    return function (target: Function, methodName: string, descriptor: any) {
        const method = descriptor.value;
        descriptor.value = function (...args: any[]) {
            try {
                let result = method.apply(this, args);
                // Check if method is asynchronous
                if (result && result instanceof Promise) {
                    // Return promise
                    return result.catch((error: any) => {
                        handleError(error, methodName, args, target.constructor.name);
                    });
                }
                if(result && result instanceof Observable ){
                    console.log(methodName);
                    result.pipe(catchError((error: any) => {
                        console.log(error);
                        handleError(error, methodName, args, this.constructor.name);
                        return result
                    }))                
                }
                // Return actual result
                return result;
            } catch (error:any) {
                handleError(error, methodName, args, target.constructor.name);
            }
        }
        return descriptor;
    }
}

I want to catch this throw('this is an error'); error on this sample. any suggestions to do this?

about 4 years ago · Juan Pablo Isaza
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