Client: fix error display for component
authorChocobozzz <florian.bigard@gmail.com>
Tue, 23 Aug 2016 15:42:56 +0000 (17:42 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 23 Aug 2016 15:42:56 +0000 (17:42 +0200)
client/src/app/admin/users/shared/user.service.ts
client/src/app/admin/users/user-add/user-add.component.ts
client/src/app/admin/users/user-list/user-list.component.ts
client/src/app/shared/rest/rest-extractor.service.ts
client/src/app/videos/video-list/video-list.component.ts
client/src/app/videos/video-watch/video-watch.component.ts
client/tsconfig.json
server/middlewares/validators/users.js
server/tests/api/check-params.js

index d96db4575d8638cdf643d6d9043236fca91b7616..13be553c01ce7b0db239a3339fd5c576a447b695 100644 (file)
@@ -20,7 +20,7 @@ export class UserService {
 
     return this.authHttp.post(UserService.BASE_USERS_URL, body)
                         .map(this.restExtractor.extractDataBool)
-                        .catch((res) => this.restExtractor.handleError(res));
+                        .catch(this.restExtractor.handleError);
   }
 
   getUsers() {
index b7efd3a8004c68a794c7d538a5f2ba3412747af8..8dd98cc5c60e13f9451248953410d5d85178033e 100644 (file)
@@ -31,7 +31,7 @@ export class UserAddComponent implements OnInit {
     this.userService.addUser(this.username, this.password).subscribe(
       ok => this.router.navigate([ '/admin/users/list' ]),
 
-      err => this.error = err
+      err => this.error = err.text
     );
   }
 }
index 598daa42aa0794749497bf6d2743f3876b16ff84..c89a61bcadd7aa0e89e7827875ec6435b553a552 100644 (file)
@@ -27,7 +27,7 @@ export class UserListComponent implements OnInit {
         this.totalUsers = totalUsers;
       },
 
-      err => alert(err)
+      err => alert(err.text)
     );
   }
 
@@ -37,7 +37,7 @@ export class UserListComponent implements OnInit {
       this.userService.removeUser(user).subscribe(
         () => this.getUsers(),
 
-        err => alert(err)
+        err => alert(err.text)
       );
     }
   }
index aa44799af47a950e2bf878c835b891f44f502206..fcb1598f4393385a78521c7e8225cd83c73e5abc 100644 (file)
@@ -34,13 +34,19 @@ export class RestExtractor {
   handleError(res: Response) {
     let text = 'Server error: ';
     text += res.text();
-    let json = res.json();
+    let json = '';
+
+    try {
+      json = res.json();
+    } catch (err) { ; }
 
     const error = {
       json,
       text
     };
 
+    console.error(error);
+
     return Observable.throw(error);
   }
 }
index 1324a6214f0f364df364308529665c3aeea2cd92..9a9ffe29fd441efcf29e619a90641fa48c6d1e51 100644 (file)
@@ -98,7 +98,7 @@ export class VideoListComponent implements OnInit, OnDestroy {
 
         this.loading.next(false);
       },
-      error => alert(error)
+      error => alert(error.text)
     );
   }
 
index bc0e3157dcf7a4f56dd8b17f7552979429dd90e6..d260e55c7c71050e3c11cd6c0612d86ec1cde640 100644 (file)
@@ -86,7 +86,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
           this.video = video;
           this.loadVideo();
         },
-        error => alert(error)
+        error => alert(error.text)
       );
     });
   }
index 60ca1422195167f883ad5c41a641b92b97133de2..263316c4fe30e0b0ff357ac0ca2b78414c3d4bdb 100644 (file)
     "src/app/shared/form-validators/url.validator.ts",
     "src/app/shared/index.ts",
     "src/app/shared/rest/index.ts",
-    "src/app/shared/rest/mock-rest-table.ts",
     "src/app/shared/rest/rest-extractor.service.ts",
-    "src/app/shared/rest/rest-filter.model.ts",
     "src/app/shared/rest/rest-pagination.ts",
-    "src/app/shared/rest/rest-sort.ts",
-    "src/app/shared/rest/rest-table-page.ts",
-    "src/app/shared/rest/rest-table.spec.ts",
-    "src/app/shared/rest/rest-table.ts",
     "src/app/shared/rest/rest.service.ts",
     "src/app/shared/search/index.ts",
     "src/app/shared/search/search-field.type.ts",
@@ -89,7 +83,6 @@
     "src/app/videos/shared/index.ts",
     "src/app/videos/shared/loader/index.ts",
     "src/app/videos/shared/loader/loader.component.ts",
-    "src/app/videos/shared/pagination.model.ts",
     "src/app/videos/shared/sort-field.type.ts",
     "src/app/videos/shared/video.model.ts",
     "src/app/videos/shared/video.service.ts",
index e540ab0d1f742af95b9022180bacb8a32b2e243f..5defdf4e30d8ace80888db8cb4719684658fe49f 100644 (file)
@@ -17,11 +17,20 @@ function usersAdd (req, res, next) {
   req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
   req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
 
-  // TODO: check we don't have already the same username
-
   logger.debug('Checking usersAdd parameters', { parameters: req.body })
 
-  checkErrors(req, res, next)
+  checkErrors(req, res, function () {
+    User.loadByUsername(req.body.username, function (err, user) {
+      if (err) {
+        logger.error('Error in usersAdd request validator.', { error: err })
+        return res.sendStatus(500)
+      }
+
+      if (user) return res.status(409).send('User already exists.')
+
+      next()
+    })
+  })
 }
 
 function usersRemove (req, res, next) {
index 4f7b26561d7354b9a40084f07ee71c0c59459a7f..e361147bbd5c32a421e3c220765da5f50e482d36 100644 (file)
@@ -590,6 +590,17 @@ describe('Test parameters validator', function () {
         requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 204)
       })
 
+      it('Should fail if we add a user with the same username', function (done) {
+        it('Should succeed with the correct params', function (done) {
+          const data = {
+            username: 'user1',
+            password: 'my super password'
+          }
+
+          requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 409)
+        })
+      })
+
       it('Should fail with a non admin user', function (done) {
         server.user = {
           username: 'user1',