Guest User

Untitled

a guest
Jan 18th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2.  
  3. import { ToastyService } from 'ng2-toasty';
  4.  
  5. @Injectable()
  6. export class ErroHandlerService {
  7.  
  8. constructor(private toasty: ToastyService) { }
  9.  
  10. handle(errorResponse: any) {
  11. let msg: string;
  12.  
  13. if (typeof errorResponse === 'string') {
  14. msg = errorResponse;
  15. } else {
  16. msg = 'Erro ao processar serviço remoto. Tente novamente.';
  17. console.error('Ocorreu um erro', errorResponse);
  18. }
  19.  
  20. this.toasty.error(msg);
  21. }
  22.  
  23. }
  24.  
  25. import { Router } from '@angular/router';
  26. import { Injectable } from '@angular/core';
  27. import { Response } from '@angular/http';
  28.  
  29. import { NotAuthenticatedError } from './../seguranca/money-http';
  30. import { ToastyService } from 'ng2-toasty';
  31.  
  32. @Injectable()
  33. export class ErrorHandlerService {
  34.  
  35. constructor(
  36. private toasty: ToastyService,
  37. private router: Router
  38. ) { }
  39.  
  40. handle(errorResponse: any) {
  41. let msg: string;
  42.  
  43. if (typeof errorResponse === 'string') {
  44. msg = errorResponse;
  45.  
  46. } else if (errorResponse instanceof NotAuthenticatedError) {
  47. msg = 'Sua sessão expirou!';
  48. this.router.navigate(['/login']);
  49.  
  50. } else if (errorResponse instanceof Response
  51. && errorResponse.status >= 400 && errorResponse.status <= 499) {
  52. let errors;
  53. msg = 'Ocorreu um erro ao processar a sua solicitação';
  54.  
  55. if (errorResponse.status === 403) {
  56. msg = 'Você não tem permissão para executar esta ação';
  57. }
  58.  
  59. try {
  60. errors = errorResponse.json();
  61.  
  62. msg = errors[0].mensagemUsuario;
  63. } catch (e) { }
  64.  
  65. console.error('Ocorreu um erro', errorResponse);
  66.  
  67. } else {
  68. msg = 'Erro ao processar serviço remoto. Tente novamente.';
  69. console.error('Ocorreu um erro', errorResponse);
  70. }
  71.  
  72. this.toasty.error(msg);
  73. }
  74.  
  75. }
Add Comment
Please, Sign In to add comment