mgechev / aspect.js

JavaScript library for aspect-oriented programming using modern syntax.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not able to Call Http POST Request in LoggerAspect Class

Mithun048 opened this issue · comments

Hi Mgechev,

I have an issue in sending http request from the @onThrowOfMethod, I am not able to see any error in console as well as any request being sent in network tab of developer tools. Kindly help on this.

Thanks,
Mithun

Hey Mithun, thanks for giving the library a try! Would you provide a code snippet so I can see how you're configuring the aspect?

Hi Mgechev, PFB the code snippet. Since "this" of the class that we use in constructor to initialize the Http is out of scope within decorator function, I am assiging it to a Static Variable and accessing it within the Decorator method ( like LoggerAspect.http).

import { Injectable} from '@angular/core';
import {beforeMethod, onThrowOfMethod, afterMethod, aroundMethod, Metadata, Wove} from 'aspect.js/aspect';
import {Http,Response} from "@angular/http";

@Injectable()
export class LoggerAspect {
    private static http: Http;
    constructor(http1: Http) {
       LoggerAspect.http=http1;
    }
  @onThrowOfMethod({classNamePattern:  /.*/,   methodNamePattern:  /.*/})
  invokeOnGenericError(meta: Metadata){
      console.log('GENERIC ERROR',meta);
       let requestData:any = {action:'sampleaction',requestMapping:"sample",data:"xxxx"};
          LoggerAspect.http.post("/error",requestData);
        //this.callService();
  } 
  callService(){
      console.log("calling error service");
      let action:string   = "sampleService" ; 
      let requestData:any = {action:'sampleaction',requestMapping:"sample",data:"xxxx"}; 
      LoggerAspect.http.post("/error",requestData); //  EVEN THIS IS NOT WORKING
  }
}

I have declared the LoggerAspect in a module as a provider inorder for the constructor to be invoked to initialize http within constructor.

@Mithun048 you cannot inject dependencies in the aspects since they are not instantiated by the Angular's injector. As workaround, you can inject the Injector in the services that you wove and later access it using meta.method.context.injector. This way you can get reference to an instance of the Http class by: meta.method.context.injector.get(Http).

Because of that problem I developed the Angular bindings for aspect.js but they don't work well with the Angular's AoT compilation, because there the DI calls are generated build-time.

I'll work on finding a solution somewhen next year. Until then I'm using the workaround described above.

Thanks Minko Gechev for prompt answer. I have a question in Woving.. Whether is it required to Wove for all the components that we want to handle or Is there any option to Wove at App Component level in order to handle the error at all child level components ?? I tried to add at App Component, but it didn't work for me. May be i could have missed something.. Could you pls give your suggestion on that...

You will need to place @Wove decorator on top of all components that you want to be woven and they should be matched by the pointcut's preconditions.

@mgechev This Error Logging functionality is not working in IE Edge for me. Any suggestions from your end would be good for us...

@Mithun048 would you post a demo on plnkr so I can see what's wrong?