Skip to content

Commit

Permalink
Tratando erros 403
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscolopes committed May 5, 2018
1 parent e2266e0 commit d20e166
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
39 changes: 27 additions & 12 deletions src/interceptors/error-interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
import { Injectable } from '@angular/core';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HTTP_INTERCEPTORS } from '@angular/common/http';
import { Observable } from 'rxjs/Rx'; // IMPORTANTE: IMPORT ATUALIZADO
import { StorageService } from '../services/storage.service';

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {

constructor(public storage: StorageService) {
}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req)
.catch((error, caught) => {
.catch((error, caught) => {

let errorObj = error;
if (errorObj.error) {
errorObj = errorObj.error;
}
if (!errorObj.status) {
errorObj = JSON.parse(errorObj);
}
let errorObj = error;
if (errorObj.error) {
errorObj = errorObj.error;
}
if (!errorObj.status) {
errorObj = JSON.parse(errorObj);
}

console.log("Erro detectado pelo interceptor:");
console.log(errorObj);
console.log("Erro detectado pelo interceptor:");
console.log(errorObj);

switch (errorObj.status) {
case 403:
this.handle403();
break;
}

return Observable.throw(errorObj);
}) as any;
}

return Observable.throw(errorObj);
}) as any;
handle403() {
this.storage.setLocalUser(null);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/pages/profile/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ export class ProfilePage {
this.cliente = response;
this.getImageIfExists();
},
error => { });
error => {
if (error.status == 403) {
this.navCtrl.setRoot('HomePage');
}
});
}
else {
this.navCtrl.setRoot('HomePage');
}
}

Expand Down

0 comments on commit d20e166

Please sign in to comment.