<commit_before>import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { AuthenticationState, WinecellarState } from '../statemanagement/state';
import { Store } from '@ngrx/store';
import { DisableBusyFlagAction, EnableBusyFlagAction } from '../statemanagement/actions';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/mergeMap';

@Injectable()
export class WinecellarHttpInterceptor implements HttpInterceptor {
  private activeCalls = 0;

  constructor(private store: Store<WinecellarState>) {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.httpCallRequested();
    return this.store
      .select(state => state.authentication) // fetch the authentication from the store as a stream
      .take(1) // we don't want this stream to keep on living (take the current value)
      .flatMap((authState: AuthenticationState) => {
        const requestToHandle = authState.isAuthenticated
          ? request.clone({
            headers: request.headers.set('authorization', `Bearer ${authState.jwtToken}`)
          })
          : request;
        return next.handle(requestToHandle);
      })
      .finally(() => this.httpCallReady());
  }

  private httpCallReady(): void {
    this.activeCalls--;
    if (this.activeCalls === 0) {
      this.store.dispatch(new DisableBusyFlagAction());
    }
  }

  private httpCallRequested(): void {
    if (this.activeCalls === 0) {
      this.store.dispatch(new EnableBusyFlagAction());
    }
    this.activeCalls++;
  }
}<commit_msg>error handling<commit_after>import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { AuthenticationState, WinecellarState } from '../statemanagement/state';
import { Store } from '@ngrx/store';
import { DisableBusyFlagAction, EnableBusyFlagAction } from '../statemanagement/actions';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/finally';
import 'rxjs/add/operator/take';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/empty';

@Injectable()
export class WinecellarHttpInterceptor implements HttpInterceptor {
  private activeCalls = 0;

  constructor(private store: Store<WinecellarState>) {
  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.httpCallRequested();
    return this.store
      .select(state => state.authentication) // fetch the authentication from the store as a stream
      .take(1) // we don't want this stream to keep on living (take the current value)
      .flatMap((authState: AuthenticationState) => {
        const requestToHandle = authState.isAuthenticated
          ? request.clone({
            headers: request.headers.set('authorization', `Bearer ${authState.jwtToken}`)
          })
          : request;
        return next.handle(requestToHandle);
      })
      .catch(() => {
        alert('Something went terribly wrong!');
        return Observable.empty();
      })
      .finally(() => this.httpCallReady());
  }

  private httpCallReady(): void {
    this.activeCalls--;
    if (this.activeCalls === 0) {
      this.store.dispatch(new DisableBusyFlagAction());
    }
  }

  private httpCallRequested(): void {
    if (this.activeCalls === 0) {
      this.store.dispatch(new EnableBusyFlagAction());
    }
    this.activeCalls++;
  }
}
{'fraction_non_alphanumeric': 0.12961421038023868, 'fraction_numerical': 0.0022203719122953097, 'mean_word_length': 3.7049608355091386, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4257725', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1036, 'n_words': 238}