From c0ec34f98d6ff8075bebd9c6ef99f6d4d5355a54 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Tue, 15 Mar 2016 16:57:27 -0300 Subject: [PATCH] Refactor error method from notification component --- src/app/components/notification/notification.component.spec.ts | 4 ++-- src/app/components/notification/notification.component.ts | 17 ++++++++++------- src/app/profile/profile.component.spec.ts | 4 ++-- src/app/profile/profile.component.ts | 2 +- src/languages/en.json | 6 ++++-- src/languages/pt.json | 6 ++++-- 6 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/app/components/notification/notification.component.spec.ts b/src/app/components/notification/notification.component.spec.ts index 22e7fd6..4cd18cd 100644 --- a/src/app/components/notification/notification.component.spec.ts +++ b/src/app/components/notification/notification.component.spec.ts @@ -20,7 +20,7 @@ describe("Components", () => { let component: Notification = new Notification(helpers.mocks.$log, sweetAlert, helpers.mocks.$translate); component.httpError(500, {}); expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({ - text: Notification.DEFAULT_HTTP_ERROR_MESSAGE, + text: Notification.DEFAULT_ERROR_MESSAGE, type: "error" })); done(); @@ -33,7 +33,7 @@ describe("Components", () => { let component: Notification = new Notification(helpers.mocks.$log, sweetAlert, helpers.mocks.$translate); component.httpError(500, null); expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({ - text: Notification.DEFAULT_HTTP_ERROR_MESSAGE, + text: Notification.DEFAULT_ERROR_MESSAGE, type: "error" })); done(); diff --git a/src/app/components/notification/notification.component.ts b/src/app/components/notification/notification.component.ts index 3f8a513..725d3c6 100644 --- a/src/app/components/notification/notification.component.ts +++ b/src/app/components/notification/notification.component.ts @@ -10,19 +10,22 @@ export class Notification { private $translate: angular.translate.ITranslateService ) { } - public static DEFAULT_HTTP_ERROR_TITLE = "notification.http-error.default.title"; - public static DEFAULT_HTTP_ERROR_MESSAGE = "notification.http-error.default.message"; + public static DEFAULT_ERROR_TITLE = "notification.error.default.title"; + public static DEFAULT_ERROR_MESSAGE = "notification.error.default.message"; public static DEFAULT_SUCCESS_TIMER = 1000; - httpError(status: number, data: any): boolean { - this.$log.debug(status, data); - - let message = (data || {}).message || Notification.DEFAULT_HTTP_ERROR_MESSAGE; + error(message: string = Notification.DEFAULT_ERROR_MESSAGE, title: string = Notification.DEFAULT_ERROR_TITLE) { + this.$log.debug("Notification error:", title, message); this.SweetAlert.swal({ - title: this.$translate.instant(Notification.DEFAULT_HTTP_ERROR_TITLE), + title: this.$translate.instant(title), text: this.$translate.instant(message), type: "error" }); + } + + httpError(status: number, data: any): boolean { + let message = (data || {}).message || Notification.DEFAULT_ERROR_MESSAGE; + this.error(`notification.http_error.${status}.message`); return true; // return true to indicate that the error was already handled } diff --git a/src/app/profile/profile.component.spec.ts b/src/app/profile/profile.component.spec.ts index 330d357..70f35f8 100644 --- a/src/app/profile/profile.component.spec.ts +++ b/src/app/profile/profile.component.spec.ts @@ -18,7 +18,7 @@ describe("Components", () => { beforeEach(() => { $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]); profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier", "getBoxes"]); - notificationMock = jasmine.createSpyObj("notificationMock", ["httpError"]); + notificationMock = jasmine.createSpyObj("notificationMock", ["error"]); let profileResponse = $q.defer(); profileResponse.resolve({ id: 1 }); @@ -54,7 +54,7 @@ describe("Components", () => { $rootScope.$apply(); expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); - expect(notificationMock.httpError).toHaveBeenCalled(); + expect(notificationMock.error).toHaveBeenCalled(); expect(component.profile).toBeUndefined(); done(); }); diff --git a/src/app/profile/profile.component.ts b/src/app/profile/profile.component.ts index 4ef53a8..ae35a27 100644 --- a/src/app/profile/profile.component.ts +++ b/src/app/profile/profile.component.ts @@ -86,7 +86,7 @@ export class Profile { }).then((response: restangular.IResponse) => { this.boxes = response.data.boxes; }).catch(() => { - notification.httpError(404, { message: "Profile not found!" }); + notification.error("notification.profile.not_found"); }); } } diff --git a/src/languages/en.json b/src/languages/en.json index c0761f7..1bdba4b 100644 --- a/src/languages/en.json +++ b/src/languages/en.json @@ -18,6 +18,8 @@ "auth.form.password": "Password", "auth.form.login_button": "Login", "navbar.content_viewer_actions.new_post": "New Post", - "notification.http-error.default.message": "Something went wrong!", - "notification.http-error.default.title": "Oops..." + "notification.error.default.message": "Something went wrong!", + "notification.error.default.title": "Oops...", + "notification.profile.not_found": "Profile not found", + "notification.http_error.401.message": "Unauthorized" } diff --git a/src/languages/pt.json b/src/languages/pt.json index 5c0c43d..26315c7 100644 --- a/src/languages/pt.json +++ b/src/languages/pt.json @@ -18,6 +18,8 @@ "auth.form.password": "Senha", "auth.form.login_button": "Login", "navbar.content_viewer_actions.new_post": "Novo Artigo", - "notification.http-error.default.message": "Algo deu errado!", - "notification.http-error.default.title": "Oops..." + "notification.error.default.message": "Algo deu errado!", + "notification.error.default.title": "Oops...", + "notification.profile.not_found": "Perfil não encontrado", + "notification.http_error.401.message": "Não autorizado" } -- libgit2 0.21.2