Skip to content

Commit

Permalink
Improve thrown errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SachaG committed Apr 26, 2018
1 parent 3e0d67b commit de981a7
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions packages/example-forum/lib/server/posts/callbacks/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ function PostsNewRateLimit (post, user) {
// check that user waits more than X seconds between posts
if(timeSinceLastPost < postInterval){
const RateLimitError = createError('posts.rate_limit_error', {message: 'posts.rate_limit_error'});
throw new RateLimitError({data: {break: true, value: postInterval-timeSinceLastPost}});
throw new RateLimitError({data: {break: true, id: 'posts.rate_limit_error', properties: { value: postInterval-timeSinceLastPost }}});

}
// check that the user doesn't post more than Y posts per day
if(numberOfPostsInPast24Hours >= maxPostsPer24Hours){
const RateLimitError = createError('posts.max_per_day', {message: 'posts.max_per_day'});
throw new RateLimitError({data: {break: true, value: maxPostsPer24Hours}});
throw new RateLimitError({data: {break: true, id: 'posts.max_per_day', properties: { value: maxPostsPer24Hours }}});
}
}

Expand All @@ -46,7 +47,14 @@ addCallback('posts.new.validate', PostsNewRateLimit);
function PostsNewDuplicateLinksCheck (post, user) {
if(!!post.url && Posts.checkForSameUrl(post.url)) {
const DuplicateError = createError('posts.link_already_posted', {message: 'posts.link_already_posted'});
throw new DuplicateError({data: {break: true, url: post.url}});
throw new DuplicateError({
data: {
break: true,
id: 'posts.link_already_posted',
path: 'url',
properties: { url: post.url },
},
});
}
return post;
}
Expand All @@ -57,9 +65,19 @@ addCallback('posts.new.sync', PostsNewDuplicateLinksCheck);
* @summary Check for duplicate links
*/
function PostsEditDuplicateLinksCheck (modifier, post) {
if(post.url !== modifier.$set.url && !!modifier.$set.url) {
Posts.checkForSameUrl(modifier.$set.url);
}
// if(post.url !== modifier.$set.url && !!modifier.$set.url) {
if (Posts.checkForSameUrl(modifier.$set.url)){
const DuplicateError = createError('posts.link_already_posted', {message: 'posts.link_already_posted'});
throw new DuplicateError({
data: {
break: true,
id: 'posts.link_already_posted',
path: 'url',
properties: { url: post.url },
},
});
}
// }
return modifier;
}
addCallback('posts.edit.sync', PostsEditDuplicateLinksCheck);

0 comments on commit de981a7

Please sign in to comment.