Angular 6 Interceptor not working as expected












0















I have written an Http Interceptor in Angular 6 .
The goal is to reload the home page when 302 status is returned. (any back end API returning 302 status )
I am currently not able to understand why the Interceptor is behaving the way it is.



Below is the code :



@Injectable()
export class InvalidResponseHandler implements HttpInterceptor{


constructor(){

}

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


return next.handle(request).pipe(


map((event: HttpEvent<any>) => {

if(event instanceof HttpResponse){
if(event.status === 302){
console.log('HttpResponse 302 status ');
window.location.reload();
return EMPTY;
}
}

})
)

}

}


Actual Behaviour :



1)I am first accessing the home page explicitly. (e.g: /home)



2)But the strange thing is , the initial call to fetch home age itself is not happening



3)It seems request is blocked reaching the server.



4)When interceptor is removed , this initial call is happening (/home)



I am not able to understand why is this behaviour ?



Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .



It should be intercepted only when HttpResponse is returned with 302 error code .



Can anybody please help here ?










share|improve this question



























    0















    I have written an Http Interceptor in Angular 6 .
    The goal is to reload the home page when 302 status is returned. (any back end API returning 302 status )
    I am currently not able to understand why the Interceptor is behaving the way it is.



    Below is the code :



    @Injectable()
    export class InvalidResponseHandler implements HttpInterceptor{


    constructor(){

    }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


    return next.handle(request).pipe(


    map((event: HttpEvent<any>) => {

    if(event instanceof HttpResponse){
    if(event.status === 302){
    console.log('HttpResponse 302 status ');
    window.location.reload();
    return EMPTY;
    }
    }

    })
    )

    }

    }


    Actual Behaviour :



    1)I am first accessing the home page explicitly. (e.g: /home)



    2)But the strange thing is , the initial call to fetch home age itself is not happening



    3)It seems request is blocked reaching the server.



    4)When interceptor is removed , this initial call is happening (/home)



    I am not able to understand why is this behaviour ?



    Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .



    It should be intercepted only when HttpResponse is returned with 302 error code .



    Can anybody please help here ?










    share|improve this question

























      0












      0








      0








      I have written an Http Interceptor in Angular 6 .
      The goal is to reload the home page when 302 status is returned. (any back end API returning 302 status )
      I am currently not able to understand why the Interceptor is behaving the way it is.



      Below is the code :



      @Injectable()
      export class InvalidResponseHandler implements HttpInterceptor{


      constructor(){

      }

      intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


      return next.handle(request).pipe(


      map((event: HttpEvent<any>) => {

      if(event instanceof HttpResponse){
      if(event.status === 302){
      console.log('HttpResponse 302 status ');
      window.location.reload();
      return EMPTY;
      }
      }

      })
      )

      }

      }


      Actual Behaviour :



      1)I am first accessing the home page explicitly. (e.g: /home)



      2)But the strange thing is , the initial call to fetch home age itself is not happening



      3)It seems request is blocked reaching the server.



      4)When interceptor is removed , this initial call is happening (/home)



      I am not able to understand why is this behaviour ?



      Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .



      It should be intercepted only when HttpResponse is returned with 302 error code .



      Can anybody please help here ?










      share|improve this question














      I have written an Http Interceptor in Angular 6 .
      The goal is to reload the home page when 302 status is returned. (any back end API returning 302 status )
      I am currently not able to understand why the Interceptor is behaving the way it is.



      Below is the code :



      @Injectable()
      export class InvalidResponseHandler implements HttpInterceptor{


      constructor(){

      }

      intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {


      return next.handle(request).pipe(


      map((event: HttpEvent<any>) => {

      if(event instanceof HttpResponse){
      if(event.status === 302){
      console.log('HttpResponse 302 status ');
      window.location.reload();
      return EMPTY;
      }
      }

      })
      )

      }

      }


      Actual Behaviour :



      1)I am first accessing the home page explicitly. (e.g: /home)



      2)But the strange thing is , the initial call to fetch home age itself is not happening



      3)It seems request is blocked reaching the server.



      4)When interceptor is removed , this initial call is happening (/home)



      I am not able to understand why is this behaviour ?



      Why the request is getting blocked as I am NOT tampering with request it should be passed as is to back end rest end point .



      It should be intercepted only when HttpResponse is returned with 302 error code .



      Can anybody please help here ?







      angular6 angular-http-interceptors






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 9:36









      AtulAtul

      6661440




      6661440
























          1 Answer
          1






          active

          oldest

          votes


















          1














          The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.



                   map((event: HttpEvent<any>) => {

          if(event instanceof HttpResponse){
          if(event.status === 302){
          console.log('HttpResponse 302 status ');
          window.location.reload();
          return EMPTY;
          }
          }

          return event; // here you return value back
          })





          share|improve this answer
























          • Hi Dmitriy , It was right . Returning event from map operator resolved the issue

            – Atul
            Jan 21 at 11:31













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265762%2fangular-6-interceptor-not-working-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.



                   map((event: HttpEvent<any>) => {

          if(event instanceof HttpResponse){
          if(event.status === 302){
          console.log('HttpResponse 302 status ');
          window.location.reload();
          return EMPTY;
          }
          }

          return event; // here you return value back
          })





          share|improve this answer
























          • Hi Dmitriy , It was right . Returning event from map operator resolved the issue

            – Atul
            Jan 21 at 11:31


















          1














          The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.



                   map((event: HttpEvent<any>) => {

          if(event instanceof HttpResponse){
          if(event.status === 302){
          console.log('HttpResponse 302 status ');
          window.location.reload();
          return EMPTY;
          }
          }

          return event; // here you return value back
          })





          share|improve this answer
























          • Hi Dmitriy , It was right . Returning event from map operator resolved the issue

            – Atul
            Jan 21 at 11:31
















          1












          1








          1







          The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.



                   map((event: HttpEvent<any>) => {

          if(event instanceof HttpResponse){
          if(event.status === 302){
          console.log('HttpResponse 302 status ');
          window.location.reload();
          return EMPTY;
          }
          }

          return event; // here you return value back
          })





          share|improve this answer













          The problem is that you forgot to return value in map operator. The main goal of map operator is to modify data and RETURN it back.



                   map((event: HttpEvent<any>) => {

          if(event instanceof HttpResponse){
          if(event.status === 302){
          console.log('HttpResponse 302 status ');
          window.location.reload();
          return EMPTY;
          }
          }

          return event; // here you return value back
          })






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 19:12









          Dmitriy KavraiskyiDmitriy Kavraiskyi

          11810




          11810













          • Hi Dmitriy , It was right . Returning event from map operator resolved the issue

            – Atul
            Jan 21 at 11:31





















          • Hi Dmitriy , It was right . Returning event from map operator resolved the issue

            – Atul
            Jan 21 at 11:31



















          Hi Dmitriy , It was right . Returning event from map operator resolved the issue

          – Atul
          Jan 21 at 11:31







          Hi Dmitriy , It was right . Returning event from map operator resolved the issue

          – Atul
          Jan 21 at 11:31




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54265762%2fangular-6-interceptor-not-working-as-expected%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre