Angular2 user request

I use a custom HTTP request class to add an authorization header to all my requests, this works fine on almost every Android device. The wired thing is that I received complaints from some clients that they receive the error message “No Internet connection”, although they have a working network connection (other applications work, and errors are also sent to Sentry servers). Since I use Sentry's error tracking, I found out that all of these clients get an error because a timeout error occurs after 10 seconds for the first request when the application starts. I figured something was wrong with this request, so I created an alpha version for a limited number of users to track the error (I send the parameters of each request to Sentry), but the requests look great.The following assumption was that something was wrong with the cordova-plugin-nativestorage on these devices, but since I catch them, it should return an empty token for rent. I don’t know how to fix it right now. Any advice is appreciated!

export class CustomRequest {
  apiToken: string = '';

  constructor(private http: Http) { }

  protected request(options: any): Observable<any> {

    // If Native Storage doens't find a token, return an empty
    let errorNativeStorage$ = function (): Observable<any> {
      return Observable.of({ Token: '' });
    };

    // Get Token form Native Storage
    let token$ = Observable.fromPromise(NativeStorage.getItem('JWT'))
      .catch(errorNativeStorage$);

    // Handle request errors
    let genericError$ = function (error: Response | any): Observable<any> {
      let errMsg: string;
      if (error instanceof Response) {
        const body = error.json() || '';
        const err = body.error || JSON.stringify(body);
        errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
      } else {
        errMsg = error.message ? error.message : error.toString();
      }

      console.error(errMsg);
      Raven.captureException(error, { extra: { errorMsg: errMsg } });

      return Observable.of({
        Errors: { General: 'No internet connection.' }
      });
    };

    // the request
    let request$ = (options) => {
      return this.http.request(new Request(options))
        .retryWhen(error => error.delay(1000))
        .timeout(10000, new Error('timeout'))
        .map((res: Response) => res.json())
        .catch(genericError$);
    };

    // get the token and build request
    return token$
      .map(jwt => {
        if (options.body) {
          if (typeof options.body !== 'string') {
            options.body = JSON.stringify(options.body);
          }
          options.body = options.body.replace(/ /g, '').replace(/\r?\n|\r/g,   '');
        }

        options.headers = new Headers({
          'Content-Type': 'application/x-www-form-urlencoded,      application/json'
        });

        if (jwt.Token) {
          options.headers.append('Authorization', `Bearer ${jwt.Token}`);
        }

        Raven.captureMessage('request options', { level: 'info',    environment: 'live', extra: options });
        return options;
      })
      .switchMap(options => request$(options));
  }
}

:

  • Ionic 2.0.0-beta.11
  • Angular 2.0.0-rc.4
  • NativeStorage github

( , ):

  • Samsung SM-N910F (Webview: Chrome Mobile 53.0.2785, Android 6.0.1)
  • Samsung SM-G800F (-: Chrome Mobile 53.0.2785, Android 5.1.1)
+4
1

- : , , Android, - Chrome Chrome Angular, (). , web- !

0

Source: https://habr.com/ru/post/1664564/


All Articles