Exibindo erros do servidor
Será apresentado aqui como tratar e exibir os erros vindos do servidor utilizando o serviço ExceptionService do Frontend Demoiselle.
Utilizaremos o serviço de notificação da aplicação para exibição dos erros Configurando e adicionando um serviço de notificação
Adicionaremos a subscrição ao Observable do serviço ExceptionService para que sejamos avisados toda vez que ocorrer um erro geral no servidor, e no tratamento, exibiremos a mensagem de erro:
import { Injectable } from '@angular/core';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { Http } from '@angular/http';
import { Subscription } from 'rxjs/Subscription';
@Injectable()
export class NotificationService {
generalErrorsSubscription: Subscription;
constructor(private toastr: ToastsManager, private http: Http, private exceptionService: ExceptionService) {
this.generalErrorsSubscription = this.exceptionService.generalErrors$.subscribe(
error => this.showGeneralErrors(error)
);
}
showGeneralErrors(errors: any) {
for (let error of errors) {
let description = '';
if (typeof error.error_description === "string") {
description = error.error_description;
} else if (typeof error.error_description === "object" && error.error_description.error_code) {
description = 'Código de erro: ' + error.error_description.error_code;
}
this.error('Erro: ' + error.error + ' ' + description);
}
}
success(text: string) {
this.toastr.success(text);
}
error(text: string) {
this.toastr.error(text);
}
info(text: string) {
this.toastr.info(text);
}
warning(text: string) {
this.toastr.warning(text);
}
}