From bf68dd752d6e3d5fce791dd8e0df9debb9d96902 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 23 Aug 2016 17:42:56 +0200 Subject: Client: fix error display for component --- client/src/app/admin/users/shared/user.service.ts | 2 +- client/src/app/admin/users/user-add/user-add.component.ts | 2 +- .../src/app/admin/users/user-list/user-list.component.ts | 4 ++-- client/src/app/shared/rest/rest-extractor.service.ts | 8 +++++++- client/src/app/videos/video-list/video-list.component.ts | 2 +- .../src/app/videos/video-watch/video-watch.component.ts | 2 +- client/tsconfig.json | 7 ------- server/middlewares/validators/users.js | 15 ++++++++++++--- server/tests/api/check-params.js | 11 +++++++++++ 9 files changed, 36 insertions(+), 17 deletions(-) diff --git a/client/src/app/admin/users/shared/user.service.ts b/client/src/app/admin/users/shared/user.service.ts index d96db4575..13be553c0 100644 --- a/client/src/app/admin/users/shared/user.service.ts +++ b/client/src/app/admin/users/shared/user.service.ts @@ -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() { diff --git a/client/src/app/admin/users/user-add/user-add.component.ts b/client/src/app/admin/users/user-add/user-add.component.ts index b7efd3a80..8dd98cc5c 100644 --- a/client/src/app/admin/users/user-add/user-add.component.ts +++ b/client/src/app/admin/users/user-add/user-add.component.ts @@ -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 ); } } diff --git a/client/src/app/admin/users/user-list/user-list.component.ts b/client/src/app/admin/users/user-list/user-list.component.ts index 598daa42a..c89a61bca 100644 --- a/client/src/app/admin/users/user-list/user-list.component.ts +++ b/client/src/app/admin/users/user-list/user-list.component.ts @@ -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) ); } } diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts index aa44799af..fcb1598f4 100644 --- a/client/src/app/shared/rest/rest-extractor.service.ts +++ b/client/src/app/shared/rest/rest-extractor.service.ts @@ -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); } } diff --git a/client/src/app/videos/video-list/video-list.component.ts b/client/src/app/videos/video-list/video-list.component.ts index 1324a6214..9a9ffe29f 100644 --- a/client/src/app/videos/video-list/video-list.component.ts +++ b/client/src/app/videos/video-list/video-list.component.ts @@ -98,7 +98,7 @@ export class VideoListComponent implements OnInit, OnDestroy { this.loading.next(false); }, - error => alert(error) + error => alert(error.text) ); } diff --git a/client/src/app/videos/video-watch/video-watch.component.ts b/client/src/app/videos/video-watch/video-watch.component.ts index bc0e3157d..d260e55c7 100644 --- a/client/src/app/videos/video-watch/video-watch.component.ts +++ b/client/src/app/videos/video-watch/video-watch.component.ts @@ -86,7 +86,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { this.video = video; this.loadVideo(); }, - error => alert(error) + error => alert(error.text) ); }); } diff --git a/client/tsconfig.json b/client/tsconfig.json index 60ca14221..263316c4f 100644 --- a/client/tsconfig.json +++ b/client/tsconfig.json @@ -69,14 +69,8 @@ "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", diff --git a/server/middlewares/validators/users.js b/server/middlewares/validators/users.js index e540ab0d1..5defdf4e3 100644 --- a/server/middlewares/validators/users.js +++ b/server/middlewares/validators/users.js @@ -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) { diff --git a/server/tests/api/check-params.js b/server/tests/api/check-params.js index 4f7b26561..e361147bb 100644 --- a/server/tests/api/check-params.js +++ b/server/tests/api/check-params.js @@ -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', -- cgit v1.2.3