Просмотр исходного кода

refactor(admin-ui): Simplify API interceptor to work with new errors

Michael Bromley 7 лет назад
Родитель
Сommit
ea8338882b
2 измененных файлов с 15 добавлено и 21 удалено
  1. 14 19
      admin-ui/src/app/data/providers/interceptor.ts
  2. 1 2
      admin-ui/src/i18n-messages/en.json

+ 14 - 19
admin-ui/src/app/data/providers/interceptor.ts

@@ -69,26 +69,21 @@ export class DefaultInterceptor implements HttpInterceptor {
             // inside the body of the response.
             const graqhQLErrors = response.body.errors;
             if (graqhQLErrors && Array.isArray(graqhQLErrors)) {
-                const firstStatus: number = graqhQLErrors[0].message.statusCode;
-                switch (firstStatus) {
-                    case 401:
-                        this.displayErrorNotification(_(`error.401-unauthorized`));
-                        break;
-                    case 403:
-                        this.authService.logOut().subscribe(() => {
-                            if (!window.location.pathname.includes('login')) {
-                                this.displayErrorNotification(_(`error.403-forbidden`));
-                            }
-                            this.router.navigate(['/login'], {
-                                queryParams: {
-                                    [AUTH_REDIRECT_PARAM]: btoa(this.router.url),
-                                },
-                            });
+                const firstCode: string = graqhQLErrors[0].extensions.code;
+                if (firstCode === 'FORBIDDEN') {
+                    this.authService.logOut().subscribe(() => {
+                        if (!window.location.pathname.includes('login')) {
+                            this.displayErrorNotification(_(`error.403-forbidden`));
+                        }
+                        this.router.navigate(['/login'], {
+                            queryParams: {
+                                [AUTH_REDIRECT_PARAM]: btoa(this.router.url),
+                            },
                         });
-                        break;
-                    default:
-                        const message = graqhQLErrors.map(err => err.message.error).join('\n');
-                        this.displayErrorNotification(message);
+                    });
+                } else {
+                    const message = graqhQLErrors.map(err => err.message).join('\n');
+                    this.displayErrorNotification(message);
                 }
             }
         }

+ 1 - 2
admin-ui/src/i18n-messages/en.json

@@ -118,7 +118,6 @@
     "verified": "Verified"
   },
   "error": {
-    "401-unauthorized": "Invalid login. Please try again",
     "403-forbidden": "Your session has expired. Please log in",
     "could-not-connect-to-server": "Could not connect to the Vendure server at { url }",
     "facet-value-form-values-do-not-match": "The number of values in the facet form does not match the actual number of values",
@@ -212,4 +211,4 @@
     "update": "Update",
     "zone": "Zone"
   }
-}
+}