Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

251
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda