client: rework promise error handling

This commit is contained in:
rr- 2017-01-08 02:12:38 +01:00
parent 5c0765c30e
commit 8a73f7e400
23 changed files with 107 additions and 199 deletions

View file

@ -85,18 +85,10 @@ class Api extends events.EventTarget {
}
loginFromCookies() {
return new Promise((resolve, reject) => {
const auth = cookies.getJSON('auth');
if (auth && auth.user && auth.password) {
this.login(auth.user, auth.password, true)
.then(resolve)
.catch(errorMessage => {
reject(errorMessage);
});
} else {
resolve();
}
});
const auth = cookies.getJSON('auth');
return auth && auth.user && auth.password ?
this.login(auth.user, auth.password, true) :
Promise.resolve();
}
login(userName, userPassword, doRemember) {
@ -117,8 +109,8 @@ class Api extends events.EventTarget {
this.user = response;
resolve();
this.dispatchEvent(new CustomEvent('login'));
}, response => {
reject(response.description || response || 'Unknown error');
}, error => {
reject(error);
this.logout();
});
});
@ -182,9 +174,9 @@ class Api extends events.EventTarget {
}
promise = promise.then(() => {
return this._rawRequest(url, requestFactory, data, {}, options);
}, errorMessage => {
}, error => {
// TODO: check if the error is because of expired uploads
return Promise.reject(errorMessage);
return Promise.reject(error);
});
promise.abort = () => abortFunction();
return promise;
@ -201,7 +193,7 @@ class Api extends events.EventTarget {
resolve(response.token);
abortFunction = () => {};
},
errorMessage => reject(errorMessage));
reject);
});
returnedPromise.abort = () => abortFunction();
return returnedPromise;
@ -253,9 +245,8 @@ class Api extends events.EventTarget {
}));
}
} catch (e) {
reject({
title: 'Authentication error',
description: 'Malformed credentials'});
reject(
new Error('Authentication error (malformed credentials)'));
}
if (!options.noProgress) {
@ -265,18 +256,19 @@ class Api extends events.EventTarget {
abortFunction = () => {
req.abort(); // does *NOT* call the callback passed in .end()
nprogress.done();
reject({
title: 'Cancelled',
description:
'The request was aborted due to user cancel.'});
reject(
new Error('The request was aborted due to user cancel.'));
};
req.end((error, response) => {
nprogress.done();
if (error) {
reject(response && response.body ? response.body : {
title: 'Networking error',
description: error.message});
if (response && response.body) {
error = new Error(
response.body.description || 'Unknown error');
error.response = response.body;
}
reject(error);
} else {
resolve(response.body);
}

View file

@ -23,8 +23,8 @@ class LoginController {
.then(() => {
const ctx = router.show('/');
ctx.controller.showSuccess('Logged in');
}, errorMessage => {
this._loginView.showError(errorMessage);
}, error => {
this._loginView.showError(error.message);
this._loginView.enableForm();
});
}

View file

@ -51,24 +51,20 @@ class CommentsController {
// TODO: disable form
e.detail.comment.text = e.detail.text;
e.detail.comment.save()
.catch(errorMessage => {
e.detail.target.showError(errorMessage);
.catch(error => {
e.detail.target.showError(error.message);
// TODO: enable form
});
}
_evtScore(e) {
e.detail.comment.setScore(e.detail.score)
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
_evtDelete(e) {
e.detail.comment.delete()
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
};

View file

@ -31,9 +31,7 @@ class HomeController {
featuringTime: info.featuringTime,
});
},
errorMessage => {
this._homeView.showError(errorMessage);
});
error => this._homeView.showError(error.message));
}
showSuccess(message) {

View file

@ -25,8 +25,8 @@ class PasswordResetController {
this._passwordResetView.showSuccess(
'E-mail has been sent. To finish the procedure, ' +
'please click the link it contains.');
}, response => {
this._passwordResetView.showError(response.description);
}, error => {
this._passwordResetView.showError(error.message);
this._passwordResetView.enableForm();
});
}
@ -41,14 +41,12 @@ class PasswordResetFinishController {
.then(response => {
password = response.password;
return api.login(name, password, false);
}, response => {
return Promise.reject(response.description);
}).then(() => {
const ctx = router.show('/');
ctx.controller.showSuccess('New password: ' + password);
}, errorMessage => {
}, error => {
const ctx = router.show('/');
ctx.controller.showError(errorMessage);
ctx.controller.showError(error.message);
});
}
}

View file

@ -17,11 +17,10 @@ class PostDetailController extends BasePostController {
Post.get(ctx.parameters.id).then(post => {
this._id = ctx.parameters.id;
post.addEventListener('change', e => this._evtSaved(e, section));
this._installView(post, section);
}, errorMessage => {
}, error => {
this._view = new EmptyView();
this._view.showError(errorMessage);
this._view.showError(error.message);
});
}
@ -46,8 +45,8 @@ class PostDetailController extends BasePostController {
Post.get(e.detail.postId).then(post => {
this._view.selectPost(post);
this._view.enableForm();
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}
@ -69,8 +68,8 @@ class PostDetailController extends BasePostController {
this._view.showSuccess('Post merged.');
router.replace(
'/post/' + e.detail.targetPost.id + '/merge', null, false);
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}

View file

@ -61,20 +61,14 @@ class PostListController {
for (let tag of this._massTagTags) {
e.detail.post.addTag(tag);
}
e.detail.post.save()
.catch(errorMessage => {
window.alert(errorMessage);
});
e.detail.post.save().catch(error => window.alert(error.message));
}
_evtUntag(e) {
for (let tag of this._massTagTags) {
e.detail.post.removeTag(tag);
}
e.detail.post.save()
.catch(errorMessage => {
window.alert(errorMessage);
});
e.detail.post.save().catch(error => window.alert(error.message));
}
_decorateSearchQuery(text) {

View file

@ -84,9 +84,9 @@ class PostMainController extends BasePostController {
this._view.commentListControl.addEventListener(
'delete', e => this._evtDeleteComment(e));
}
}, errorMessage => {
}, error => {
this._view = new EmptyView();
this._view.showError(errorMessage);
this._view.showError(error.message);
});
}
@ -117,8 +117,8 @@ class PostMainController extends BasePostController {
.then(() => {
this._view.sidebarControl.showSuccess('Post featured.');
this._view.sidebarControl.enableForm();
}, errorMessage => {
this._view.sidebarControl.showError(errorMessage);
}, error => {
this._view.sidebarControl.showError(error.message);
this._view.sidebarControl.enableForm();
});
}
@ -135,8 +135,8 @@ class PostMainController extends BasePostController {
misc.disableExitConfirmation();
const ctx = router.show('/posts');
ctx.controller.showSuccess('Post deleted.');
}, errorMessage => {
this._view.sidebarControl.showError(errorMessage);
}, error => {
this._view.sidebarControl.showError(error.message);
this._view.sidebarControl.enableForm();
});
}
@ -168,8 +168,8 @@ class PostMainController extends BasePostController {
this._view.sidebarControl.showSuccess('Post saved.');
this._view.sidebarControl.enableForm();
misc.disableExitConfirmation();
}, errorMessage => {
this._view.sidebarControl.showError(errorMessage);
}, error => {
this._view.sidebarControl.showError(error.message);
this._view.sidebarControl.enableForm();
});
}
@ -192,8 +192,8 @@ class PostMainController extends BasePostController {
this._view.commentControl.exitEditMode();
this._view.commentControl.enableForm();
misc.disableExitConfirmation();
}, errorMessage => {
this._view.commentControl.showError(errorMessage);
}, error => {
this._view.commentControl.showError(error.message);
this._view.commentControl.enableForm();
});
}
@ -202,24 +202,20 @@ class PostMainController extends BasePostController {
// TODO: disable form
e.detail.comment.text = e.detail.text;
e.detail.comment.save()
.catch(errorMessage => {
e.detail.target.showError(errorMessage);
.catch(error => {
e.detail.target.showError(error.message);
// TODO: enable form
});
}
_evtScoreComment(e) {
e.detail.comment.setScore(e.detail.score)
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
_evtDeleteComment(e) {
e.detail.comment.delete()
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
_evtScorePost(e) {
@ -227,9 +223,7 @@ class PostMainController extends BasePostController {
return;
}
e.detail.post.setScore(e.detail.score)
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
_evtFavoritePost(e) {
@ -237,9 +231,7 @@ class PostMainController extends BasePostController {
return;
}
e.detail.post.addToFavorites()
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
_evtUnfavoritePost(e) {
@ -247,9 +239,7 @@ class PostMainController extends BasePostController {
return;
}
e.detail.post.removeFromFavorites()
.catch(errorMessage => {
window.alert(errorMessage);
});
.catch(error => window.alert(error.message));
}
}

View file

@ -62,22 +62,23 @@ class PostUploadController {
misc.disableExitConfirmation();
const ctx = router.show('/posts');
ctx.controller.showSuccess('Posts uploaded.');
}, ([errorMessage, uploadable, similarPostResults]) => {
if (uploadable) {
if (similarPostResults) {
uploadable.lookalikes = similarPostResults;
this._view.updateUploadable(uploadable);
}, error => {
if (error.uploadable) {
if (error.similarPosts) {
error.uploadable.lookalikes = error.similarPosts;
this._view.updateUploadable(error.uploadable);
this._view.showInfo(genericErrorMessage);
this._view.showInfo(errorMessage, uploadable);
this._view.showInfo(
error.message, error.uploadable);
} else {
this._view.showError(genericErrorMessage);
this._view.showError(errorMessage, uploadable);
this._view.showError(
error.message, error.uploadable);
}
} else {
this._view.showError(errorMessage);
this._view.showError(error.message);
}
this._view.enableForm();
return Promise.reject();
});
}
@ -94,20 +95,21 @@ class PostUploadController {
if (searchResult) {
// notify about exact duplicate
if (searchResult.exactPost && !skipDuplicates) {
return Promise.reject([
`Post already uploaded (@${searchResult.exactPost.id})`,
uploadable,
null]);
let error = new Error('Post already uploaded ' +
`(@${searchResult.exactPost.id})`);
error.uploadable = uploadable;
return Promise.reject(error);
}
// notify about similar posts
if (!searchResult.exactPost
&& searchResult.similarPosts.length) {
return Promise.reject([
if (!searchResult.exactPost &&
searchResult.similarPosts.length) {
let error = new Error(
`Found ${searchResult.similarPosts.length} similar ` +
'posts.\nYou can resume or discard this upload.',
uploadable,
searchResult.similarPosts]);
'posts.\nYou can resume or discard this upload.');
error.uploadable = uploadable;
error.similarPosts = searchResult.similarPosts;
return Promise.reject(error);
}
}
@ -118,8 +120,6 @@ class PostUploadController {
.then(() => {
this._view.removeUploadable(uploadable);
return Promise.resolve();
}, errorMessage => {
return Promise.reject([errorMessage, uploadable, null]);
});
returnedSavePromise.abort = () => {
@ -128,8 +128,6 @@ class PostUploadController {
this._lastCancellablePromise = returnedSavePromise;
return returnedSavePromise;
}, errorMessage => {
return Promise.reject([errorMessage, uploadable, null]);
});
}

View file

@ -29,9 +29,9 @@ class TagCategoriesController {
canSetDefault: api.hasPrivilege('tagCategories:setDefault'),
});
this._view.addEventListener('submit', e => this._evtSubmit(e));
}, errorMessage => {
}, error => {
this._view = new EmptyView();
this._view.showError(errorMessage);
this._view.showError(error.message);
});
}
@ -43,9 +43,9 @@ class TagCategoriesController {
tags.refreshExport();
this._view.enableForm();
this._view.showSuccess('Changes saved.');
}, errorMessage => {
}, error => {
this._view.enableForm();
this._view.showError(errorMessage);
this._view.showError(error.message);
});
}
}

View file

@ -47,9 +47,9 @@ class TagController {
this._view.addEventListener('submit', e => this._evtUpdate(e));
this._view.addEventListener('merge', e => this._evtMerge(e));
this._view.addEventListener('delete', e => this._evtDelete(e));
}, errorMessage => {
}, error => {
this._view = new EmptyView();
this._view.showError(errorMessage);
this._view.showError(error.message);
});
}
@ -86,8 +86,8 @@ class TagController {
e.detail.tag.save().then(() => {
this._view.showSuccess('Tag saved.');
this._view.enableForm();
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}
@ -100,8 +100,8 @@ class TagController {
this._view.enableForm();
router.replace(
'/tag/' + e.detail.targetTagName + '/merge', null, false);
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}
@ -113,8 +113,8 @@ class TagController {
.then(() => {
const ctx = router.show('/tags/');
ctx.controller.showSuccess('Tag deleted.');
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}

View file

@ -63,9 +63,9 @@ class UserController {
this._view.addEventListener('change', e => this._evtChange(e));
this._view.addEventListener('submit', e => this._evtUpdate(e));
this._view.addEventListener('delete', e => this._evtDelete(e));
}, errorMessage => {
}, error => {
this._view = new EmptyView();
this._view.showError(errorMessage);
this._view.showError(error.message);
});
}
@ -115,13 +115,11 @@ class UserController {
e.detail.password || api.userPassword,
false) :
Promise.resolve();
}, errorMessage => {
return Promise.reject(errorMessage);
}).then(() => {
this._view.showSuccess('Settings updated.');
this._view.enableForm();
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}
@ -143,8 +141,8 @@ class UserController {
const ctx = router.show('/');
ctx.controller.showSuccess('Account deleted.');
}
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}

View file

@ -31,13 +31,11 @@ class UserRegistrationController {
user.save().then(() => {
api.forget();
return api.login(e.detail.name, e.detail.password, false);
}, errorMessage => {
return Promise.reject(errorMessage);
}).then(() => {
const ctx = router.show('/');
ctx.controller.showSuccess('Welcome aboard!');
}, errorMessage => {
this._view.showError(errorMessage);
}, error => {
this._view.showError(error.message);
this._view.enableForm();
});
}

View file

@ -58,7 +58,7 @@ const api = require('./api.js');
tags.refreshExport(); // we don't care about errors
api.loginFromCookies().then(() => {
router.start();
}, errorMessage => {
}, error => {
if (window.location.href.indexOf('login') !== -1) {
api.forget();
router.start();
@ -66,6 +66,6 @@ api.loginFromCookies().then(() => {
const ctx = router.start('/');
ctx.controller.showError(
'An error happened while trying to log you in: ' +
errorMessage);
error.message);
}
});

View file

@ -50,8 +50,6 @@ class Comment extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -66,8 +64,6 @@ class Comment extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -81,8 +77,6 @@ class Comment extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}

View file

@ -15,8 +15,6 @@ class Info {
Post.fromResponse(response.featuredPost) :
undefined
}));
}, response => {
return Promise.reject(response.description);
});
}
}

View file

@ -79,8 +79,6 @@ class Post extends events.EventTarget {
item.post = Post.fromResponse(item.post);
}
return Promise.resolve(response);
}, response => {
return Promise.reject(response.description);
});
returnedPromise.abort = () => {
@ -94,8 +92,6 @@ class Post extends events.EventTarget {
return api.get('/post/' + id)
.then(response => {
return Promise.resolve(Post.fromResponse(response));
}, response => {
return Promise.reject(response.description);
});
}
@ -173,12 +169,13 @@ class Post extends events.EventTarget {
new CustomEvent('changeThumbnail', {detail: {post: this}}));
}
return Promise.resolve();
}, response => {
if (response.name === 'PostAlreadyUploadedError') {
return Promise.reject(
`Post already uploaded (@${response.otherPostId})`);
}, error => {
if (error.response &&
error.response.name === 'PostAlreadyUploadedError') {
error.message =
`Post already uploaded (@${error.response.otherPostId})`;
}
return Promise.reject(response.description);
return Promise.reject(error);
});
returnedPromise.abort = () => {
@ -192,8 +189,6 @@ class Post extends events.EventTarget {
return api.post('/featured-post', {id: this._id})
.then(response => {
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -206,8 +201,6 @@ class Post extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -221,8 +214,6 @@ class Post extends events.EventTarget {
mergeTo: targetId,
replaceContent: useOldContent,
});
}, response => {
return Promise.reject(response);
}).then(response => {
this._updateFromResponse(response);
this.dispatchEvent(new CustomEvent('change', {
@ -231,8 +222,6 @@ class Post extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -254,8 +243,6 @@ class Post extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -277,8 +264,6 @@ class Post extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -300,8 +285,6 @@ class Post extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}

View file

@ -9,12 +9,7 @@ class PostList extends AbstractList {
const url =
`/post/${id}/around?fields=id` +
`&query=${encodeURIComponent(searchQuery)}`;
return api.get(url)
.then(response => {
return Promise.resolve(response);
}).catch(response => {
return Promise.reject(response.description);
});
return api.get(url);
}
static search(text, page, pageSize, fields) {

View file

@ -36,8 +36,6 @@ class Tag extends events.EventTarget {
return api.get('/tag/' + encodeURIComponent(name))
.then(response => {
return Promise.resolve(Tag.fromResponse(response));
}, response => {
return Promise.reject(response.description);
});
}
@ -73,8 +71,6 @@ class Tag extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -87,8 +83,6 @@ class Tag extends events.EventTarget {
mergeToVersion: response.version,
mergeTo: targetName,
});
}, response => {
return Promise.reject(response);
}).then(response => {
this._updateFromResponse(response);
this.dispatchEvent(new CustomEvent('change', {
@ -97,8 +91,6 @@ class Tag extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -113,8 +105,6 @@ class Tag extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}

View file

@ -58,8 +58,6 @@ class TagCategory extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -74,8 +72,6 @@ class TagCategory extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}

View file

@ -61,9 +61,6 @@ class TagCategoryList extends AbstractList {
.then(response => {
this._deletedCategories = [];
return Promise.resolve();
}, errorMessage => {
return Promise.reject(
errorMessage.description || errorMessage);
});
}

View file

@ -43,8 +43,6 @@ class User extends events.EventTarget {
return api.get('/user/' + encodeURIComponent(name))
.then(response => {
return Promise.resolve(User.fromResponse(response));
}, response => {
return Promise.reject(response.description);
});
}
@ -89,8 +87,6 @@ class User extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}
@ -105,8 +101,6 @@ class User extends events.EventTarget {
},
}));
return Promise.resolve();
}, response => {
return Promise.reject(response.description);
});
}

View file

@ -114,8 +114,8 @@ class EndlessPageView {
this._working--;
resolve(pageNode);
});
}, response => {
this.showError(response.description);
}, error => {
this.showError(error.message);
this._working--;
reject();
});