Commit c0ec34f98d6ff8075bebd9c6ef99f6d4d5355a54
1 parent
90add01a
Exists in
master
and in
1 other branch
Refactor error method from notification component
Showing
6 changed files
with
23 additions
and
16 deletions
Show diff stats
src/app/components/notification/notification.component.spec.ts
@@ -20,7 +20,7 @@ describe("Components", () => { | @@ -20,7 +20,7 @@ describe("Components", () => { | ||
20 | let component: Notification = new Notification(<any>helpers.mocks.$log, <any>sweetAlert, <any>helpers.mocks.$translate); | 20 | let component: Notification = new Notification(<any>helpers.mocks.$log, <any>sweetAlert, <any>helpers.mocks.$translate); |
21 | component.httpError(500, {}); | 21 | component.httpError(500, {}); |
22 | expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({ | 22 | expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({ |
23 | - text: Notification.DEFAULT_HTTP_ERROR_MESSAGE, | 23 | + text: Notification.DEFAULT_ERROR_MESSAGE, |
24 | type: "error" | 24 | type: "error" |
25 | })); | 25 | })); |
26 | done(); | 26 | done(); |
@@ -33,7 +33,7 @@ describe("Components", () => { | @@ -33,7 +33,7 @@ describe("Components", () => { | ||
33 | let component: Notification = new Notification(<any>helpers.mocks.$log, <any>sweetAlert, <any>helpers.mocks.$translate); | 33 | let component: Notification = new Notification(<any>helpers.mocks.$log, <any>sweetAlert, <any>helpers.mocks.$translate); |
34 | component.httpError(500, null); | 34 | component.httpError(500, null); |
35 | expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({ | 35 | expect(sweetAlert.swal).toHaveBeenCalledWith(jasmine.objectContaining({ |
36 | - text: Notification.DEFAULT_HTTP_ERROR_MESSAGE, | 36 | + text: Notification.DEFAULT_ERROR_MESSAGE, |
37 | type: "error" | 37 | type: "error" |
38 | })); | 38 | })); |
39 | done(); | 39 | done(); |
src/app/components/notification/notification.component.ts
@@ -10,19 +10,22 @@ export class Notification { | @@ -10,19 +10,22 @@ export class Notification { | ||
10 | private $translate: angular.translate.ITranslateService | 10 | private $translate: angular.translate.ITranslateService |
11 | ) { } | 11 | ) { } |
12 | 12 | ||
13 | - public static DEFAULT_HTTP_ERROR_TITLE = "notification.http-error.default.title"; | ||
14 | - public static DEFAULT_HTTP_ERROR_MESSAGE = "notification.http-error.default.message"; | 13 | + public static DEFAULT_ERROR_TITLE = "notification.error.default.title"; |
14 | + public static DEFAULT_ERROR_MESSAGE = "notification.error.default.message"; | ||
15 | public static DEFAULT_SUCCESS_TIMER = 1000; | 15 | public static DEFAULT_SUCCESS_TIMER = 1000; |
16 | 16 | ||
17 | - httpError(status: number, data: any): boolean { | ||
18 | - this.$log.debug(status, data); | ||
19 | - | ||
20 | - let message = (data || {}).message || Notification.DEFAULT_HTTP_ERROR_MESSAGE; | 17 | + error(message: string = Notification.DEFAULT_ERROR_MESSAGE, title: string = Notification.DEFAULT_ERROR_TITLE) { |
18 | + this.$log.debug("Notification error:", title, message); | ||
21 | this.SweetAlert.swal({ | 19 | this.SweetAlert.swal({ |
22 | - title: this.$translate.instant(Notification.DEFAULT_HTTP_ERROR_TITLE), | 20 | + title: this.$translate.instant(title), |
23 | text: this.$translate.instant(message), | 21 | text: this.$translate.instant(message), |
24 | type: "error" | 22 | type: "error" |
25 | }); | 23 | }); |
24 | + } | ||
25 | + | ||
26 | + httpError(status: number, data: any): boolean { | ||
27 | + let message = (data || {}).message || Notification.DEFAULT_ERROR_MESSAGE; | ||
28 | + this.error(`notification.http_error.${status}.message`); | ||
26 | return true; // return true to indicate that the error was already handled | 29 | return true; // return true to indicate that the error was already handled |
27 | } | 30 | } |
28 | 31 |
src/app/profile/profile.component.spec.ts
@@ -18,7 +18,7 @@ describe("Components", () => { | @@ -18,7 +18,7 @@ describe("Components", () => { | ||
18 | beforeEach(() => { | 18 | beforeEach(() => { |
19 | $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]); | 19 | $stateParams = jasmine.createSpyObj("$stateParams", ["profile"]); |
20 | profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier", "getBoxes"]); | 20 | profileServiceMock = jasmine.createSpyObj("profileServiceMock", ["setCurrentProfileByIdentifier", "getBoxes"]); |
21 | - notificationMock = jasmine.createSpyObj("notificationMock", ["httpError"]); | 21 | + notificationMock = jasmine.createSpyObj("notificationMock", ["error"]); |
22 | 22 | ||
23 | let profileResponse = $q.defer(); | 23 | let profileResponse = $q.defer(); |
24 | profileResponse.resolve({ id: 1 }); | 24 | profileResponse.resolve({ id: 1 }); |
@@ -54,7 +54,7 @@ describe("Components", () => { | @@ -54,7 +54,7 @@ describe("Components", () => { | ||
54 | $rootScope.$apply(); | 54 | $rootScope.$apply(); |
55 | 55 | ||
56 | expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); | 56 | expect(profileServiceMock.setCurrentProfileByIdentifier).toHaveBeenCalled(); |
57 | - expect(notificationMock.httpError).toHaveBeenCalled(); | 57 | + expect(notificationMock.error).toHaveBeenCalled(); |
58 | expect(component.profile).toBeUndefined(); | 58 | expect(component.profile).toBeUndefined(); |
59 | done(); | 59 | done(); |
60 | }); | 60 | }); |
src/app/profile/profile.component.ts
@@ -86,7 +86,7 @@ export class Profile { | @@ -86,7 +86,7 @@ export class Profile { | ||
86 | }).then((response: restangular.IResponse) => { | 86 | }).then((response: restangular.IResponse) => { |
87 | this.boxes = response.data.boxes; | 87 | this.boxes = response.data.boxes; |
88 | }).catch(() => { | 88 | }).catch(() => { |
89 | - notification.httpError(404, { message: "Profile not found!" }); | 89 | + notification.error("notification.profile.not_found"); |
90 | }); | 90 | }); |
91 | } | 91 | } |
92 | } | 92 | } |
src/languages/en.json
@@ -18,6 +18,8 @@ | @@ -18,6 +18,8 @@ | ||
18 | "auth.form.password": "Password", | 18 | "auth.form.password": "Password", |
19 | "auth.form.login_button": "Login", | 19 | "auth.form.login_button": "Login", |
20 | "navbar.content_viewer_actions.new_post": "New Post", | 20 | "navbar.content_viewer_actions.new_post": "New Post", |
21 | - "notification.http-error.default.message": "Something went wrong!", | ||
22 | - "notification.http-error.default.title": "Oops..." | 21 | + "notification.error.default.message": "Something went wrong!", |
22 | + "notification.error.default.title": "Oops...", | ||
23 | + "notification.profile.not_found": "Profile not found", | ||
24 | + "notification.http_error.401.message": "Unauthorized" | ||
23 | } | 25 | } |
src/languages/pt.json
@@ -18,6 +18,8 @@ | @@ -18,6 +18,8 @@ | ||
18 | "auth.form.password": "Senha", | 18 | "auth.form.password": "Senha", |
19 | "auth.form.login_button": "Login", | 19 | "auth.form.login_button": "Login", |
20 | "navbar.content_viewer_actions.new_post": "Novo Artigo", | 20 | "navbar.content_viewer_actions.new_post": "Novo Artigo", |
21 | - "notification.http-error.default.message": "Algo deu errado!", | ||
22 | - "notification.http-error.default.title": "Oops..." | 21 | + "notification.error.default.message": "Algo deu errado!", |
22 | + "notification.error.default.title": "Oops...", | ||
23 | + "notification.profile.not_found": "Perfil não encontrado", | ||
24 | + "notification.http_error.401.message": "Não autorizado" | ||
23 | } | 25 | } |