Client: fix error display for component

This commit is contained in:
Chocobozzz 2016-08-23 17:42:56 +02:00
parent de59c48f5f
commit bf68dd752d
9 changed files with 36 additions and 17 deletions

View File

@ -20,7 +20,7 @@ export class UserService {
return this.authHttp.post(UserService.BASE_USERS_URL, body) return this.authHttp.post(UserService.BASE_USERS_URL, body)
.map(this.restExtractor.extractDataBool) .map(this.restExtractor.extractDataBool)
.catch((res) => this.restExtractor.handleError(res)); .catch(this.restExtractor.handleError);
} }
getUsers() { getUsers() {

View File

@ -31,7 +31,7 @@ export class UserAddComponent implements OnInit {
this.userService.addUser(this.username, this.password).subscribe( this.userService.addUser(this.username, this.password).subscribe(
ok => this.router.navigate([ '/admin/users/list' ]), ok => this.router.navigate([ '/admin/users/list' ]),
err => this.error = err err => this.error = err.text
); );
} }
} }

View File

@ -27,7 +27,7 @@ export class UserListComponent implements OnInit {
this.totalUsers = totalUsers; 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.userService.removeUser(user).subscribe(
() => this.getUsers(), () => this.getUsers(),
err => alert(err) err => alert(err.text)
); );
} }
} }

View File

@ -34,13 +34,19 @@ export class RestExtractor {
handleError(res: Response) { handleError(res: Response) {
let text = 'Server error: '; let text = 'Server error: ';
text += res.text(); text += res.text();
let json = res.json(); let json = '';
try {
json = res.json();
} catch (err) { ; }
const error = { const error = {
json, json,
text text
}; };
console.error(error);
return Observable.throw(error); return Observable.throw(error);
} }
} }

View File

@ -98,7 +98,7 @@ export class VideoListComponent implements OnInit, OnDestroy {
this.loading.next(false); this.loading.next(false);
}, },
error => alert(error) error => alert(error.text)
); );
} }

View File

@ -86,7 +86,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy {
this.video = video; this.video = video;
this.loadVideo(); this.loadVideo();
}, },
error => alert(error) error => alert(error.text)
); );
}); });
} }

View File

@ -69,14 +69,8 @@
"src/app/shared/form-validators/url.validator.ts", "src/app/shared/form-validators/url.validator.ts",
"src/app/shared/index.ts", "src/app/shared/index.ts",
"src/app/shared/rest/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-extractor.service.ts",
"src/app/shared/rest/rest-filter.model.ts",
"src/app/shared/rest/rest-pagination.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/rest/rest.service.ts",
"src/app/shared/search/index.ts", "src/app/shared/search/index.ts",
"src/app/shared/search/search-field.type.ts", "src/app/shared/search/search-field.type.ts",
@ -89,7 +83,6 @@
"src/app/videos/shared/index.ts", "src/app/videos/shared/index.ts",
"src/app/videos/shared/loader/index.ts", "src/app/videos/shared/loader/index.ts",
"src/app/videos/shared/loader/loader.component.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/sort-field.type.ts",
"src/app/videos/shared/video.model.ts", "src/app/videos/shared/video.model.ts",
"src/app/videos/shared/video.service.ts", "src/app/videos/shared/video.service.ts",

View File

@ -17,11 +17,20 @@ function usersAdd (req, res, next) {
req.checkBody('username', 'Should have a valid username').isUserUsernameValid() req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
req.checkBody('password', 'Should have a valid password').isUserPasswordValid() 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 }) 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) { function usersRemove (req, res, next) {

View File

@ -590,6 +590,17 @@ describe('Test parameters validator', function () {
requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 204) 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) { it('Should fail with a non admin user', function (done) {
server.user = { server.user = {
username: 'user1', username: 'user1',