Skip to content

Commit

Permalink
Wrapped Promise.all() with try/catch block
Browse files Browse the repository at this point in the history
Wrapped Promise.all() with try/catch block because it might fail.
  • Loading branch information
liutng committed Nov 17, 2022
1 parent 2063844 commit 8055e5e
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/api/posts/src/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,26 @@ posts.get('/', validatePostsQuery(), async (req, res, next) => {
first: `/posts?per_page=${perPage}&page=${1}`,
last: `/posts?per_page=${perPage}&page=${Math.floor(postsCount / perPage)}`,
});
let data;
let data = {};
if (expand === 1) {
data = await Promise.all(
ids.map(async (id) => {
// obtain the corresponding feed to populate the author's name to the return data.
const post = await Post.byId(id);
return {
id,
url: `${postsUrl}/${id}`,
author: post.feed.author,
title: post.title,
publishDate: post.published,
};
})
);
try {
data = await Promise.all(
ids.map(async (id) => {
// obtain the corresponding feed to populate the author's name to the return data.
const post = await Post.byId(id);
return {
id,
url: `${postsUrl}/${id}`,
author: post.feed.author,
title: post.title,
publishDate: post.published,
};
})
);
} catch (err) {
logger.error({ err }, 'Unable to get detail information of posts from Redis');
next(createError(503, 'Unable to connect to database'));
}
} else
data = ids
// Return id and url for a specific post
Expand Down

0 comments on commit 8055e5e

Please sign in to comment.