Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!