Skip to content

Commit

Permalink
ch(async): wrap async await with try-catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
sojida committed Apr 3, 2019
1 parent e1ffaac commit b4889f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 60 deletions.
69 changes: 38 additions & 31 deletions server/controllers/article.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,49 @@ const controller = {
* @returns {object} response and user profile data
*/
async getOneArticle(req, res) {
if (!validations.verifyUUID(req.params.id)) {
return res.status(400).json({
error: 'id not valid',
try {
if (!validations.verifyUUID(req.params.id)) {
return res.status(400).json({
error: 'id not valid',
});
}

const article = await Article.findOne({
where: {
id: req.params.id,
},
});
}

const article = await Article.findOne({
where: {
id: req.params.id,
},
});
if (!article) {
return res.status(404).json({
error: 'Article not found',
});
}

const articleObj = {
id: article.id,
author: article.user_id,
title: article.title,
slug: article.slug,
abstract: article.abstract,
body: article.body,
category: article.category,
imageurl: article.image_url,
bookmarkcount: article.bookmark_count,
likescount: article.likes_count,
createdAt: article.createdAt,
updatedAt: article.updatedAt,
};

if (!article) {
return res.status(404).json({
error: 'Article not found',
return res.status(200).json({
data: [articleObj],
});
} catch (error) {
return res.status(500).json({
error,
message: 'Oops! There seem to be a database error',
});
}

const articleObj = {
id: article.id,
author: article.user_id,
title: article.title,
slug: article.slug,
abstract: article.abstract,
body: article.body,
category: article.category,
imageurl: article.image_url,
bookmarkcount: article.bookmark_count,
likescount: article.likes_count,
createdAt: article.createdAt,
updatedAt: article.updatedAt,
};

return res.status(200).json({
data: [articleObj],
});
},
};

Expand Down
65 changes: 36 additions & 29 deletions server/controllers/profile.controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,47 @@ const controller = {
* @returns {object} response and user profile data
*/
async getUserProfile(req, res) {
if (!validations.verifyUUID(req.params.id)) {
return res.status(400).json({
error: 'id not valid',
try {
if (!validations.verifyUUID(req.params.id)) {
return res.status(400).json({
error: 'id not valid',
});
}

const user = await User.findOne({
where: {
id: req.params.id,
},
});
}

const user = await User.findOne({
where: {
id: req.params.id,
},
});
if (!user) {
return res.status(404).json({
error: 'User not found',
});
}

const profile = {
id: user.id,
firstname: user.first_name,
lastname: user.last_name,
title: user.title,
phonenumber: user.phone_number,
email: user.email,
isreviewer: user.is_reviewer,
researchfield: user.research_field,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
};

if (!user) {
return res.status(404).json({
error: 'User not found',
return res.status(200).json({
data: [profile],
});
} catch (error) {
return res.status(500).json({
error,
message: 'Oops! There seem to be a database error',
});
}

const profile = {
id: user.id,
firstname: user.first_name,
lastname: user.last_name,
title: user.title,
phonenumber: user.phone_number,
email: user.email,
isreviewer: user.is_reviewer,
researchfield: user.research_field,
createdAt: user.createdAt,
updatedAt: user.updatedAt,
};

return res.status(200).json({
data: [profile],
});
},
};

Expand Down

0 comments on commit b4889f6

Please sign in to comment.