]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr/dashboard: Make preventDefault work with 400 errors 26561/head
authorStephan Müller <smueller@suse.com>
Thu, 21 Feb 2019 10:53:46 +0000 (11:53 +0100)
committerStephan Müller <smueller@suse.com>
Mon, 25 Mar 2019 09:12:12 +0000 (10:12 +0100)
The problem was that, if a error with the status code 400 was
received by the error interceptor the "timeoutId" was not tracked,
therefor "preventDefault" didn't prevent anything as "timeoutId"
was undefined.

Fixes: https://tracker.ceph.com/issues/38418
Signed-off-by: Stephan Müller <smueller@suse.com>
src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/api-interceptor.service.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.spec.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/services/notification.service.ts

index 67e93e2a0457a898dcd155aa1395d5f7783d5d5c..cc7fb2ba73f6b44f40e4c3393aa56248983fa69f 100644 (file)
@@ -182,6 +182,16 @@ describe('ApiInterceptorService', () => {
       expect(notificationService.save).not.toHaveBeenCalled();
     }));
 
+    it('should be able to use preventDefault with 400 errors', fakeAsync(() => {
+      httpError(
+        { task: { name: 'someName', metadata: { component: 'someComponent' } } },
+        { status: 400 },
+        (resp) => resp.preventDefault()
+      );
+      tick(10);
+      expect(notificationService.save).not.toHaveBeenCalled();
+    }));
+
     it('should prevent the default behaviour by status code', fakeAsync(() => {
       httpError(undefined, { status: 500 }, (resp) => resp.ignoreStatusCode(500));
       tick(10);
index 27d41be95d20f8b1443ea36053891f4636d61157..1e10ddd494f6310d4843ed2b9d2f59e870ee2ac5 100644 (file)
@@ -33,7 +33,7 @@ export class ApiInterceptorService implements HttpInterceptor {
     return next.handle(request).pipe(
       catchError((resp) => {
         if (resp instanceof HttpErrorResponse) {
-          let showNotification = true;
+          let timeoutId: number;
           switch (resp.status) {
             case 400:
               const finishedTask = new FinishedTask();
@@ -50,21 +50,19 @@ export class ApiInterceptorService implements HttpInterceptor {
 
               finishedTask.success = false;
               finishedTask.exception = resp.error;
-              this.notificationService.notifyTask(finishedTask);
-              showNotification = false;
+              timeoutId = this.notificationService.notifyTask(finishedTask);
               break;
             case 401:
               this.authStorageService.remove();
               this.router.navigate(['/login']);
-              showNotification = false;
               break;
             case 403:
               this.router.navigate(['/403']);
               break;
+            default:
+              timeoutId = this.prepareNotification(resp);
           }
 
-          const timeoutId = showNotification ? this.prepareNotification(resp) : undefined;
-
           /**
            * Decorated preventDefault method (in case error previously had
            * preventDefault method defined). If called, it will prevent a
index c0413f4404b272cef65e3e05989c685a5f345c75..76d75f9c83d14464ff85f8cd038896fd007ecbbe 100644 (file)
@@ -107,6 +107,16 @@ describe('NotificationService', () => {
     expect(notification.message).toBe(undefined);
   }));
 
+  it('should be able to stop notifyTask from notifying', fakeAsync(() => {
+    const task = _.assign(new FinishedTask(), {
+      success: true
+    });
+    const timeoutId = service.notifyTask(task, true);
+    service.cancel(timeoutId);
+    tick(100);
+    expect(service['dataSource'].getValue().length).toBe(0);
+  }));
+
   it('should show a error task notification', fakeAsync(() => {
     const task = _.assign(
       new FinishedTask('rbd/create', {
index 08b6ff8878b7cfd9ed9b80dacb1b790d9f52a430..14bd6b51e1b14c5f2526d3f71274235704772a26 100644 (file)
@@ -150,7 +150,7 @@ export class NotificationService {
     }"></i>`;
   }
 
-  notifyTask(finishedTask: FinishedTask, success: boolean = true) {
+  notifyTask(finishedTask: FinishedTask, success: boolean = true): number {
     let notification: CdNotificationConfig;
     if (finishedTask.success && success) {
       notification = new CdNotificationConfig(
@@ -164,7 +164,7 @@ export class NotificationService {
         this.taskMessageService.getErrorMessage(finishedTask)
       );
     }
-    this.show(notification);
+    return this.show(notification);
   }
 
   /**