Skip to content

Commit

Permalink
fix: logger.error 로그 중복 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
seohyun0120 committed Sep 21, 2024
1 parent c4771dd commit 14b95c2
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 60 deletions.
6 changes: 1 addition & 5 deletions src/controller/keyword.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ const getTopKeywords = async (req: Request, res: Response, next: NextFunction) =
const topReactionImage: string = await MemeService.getTopReactionImage(keyword);
return { ...keyword, topReactionImage } as IKeywordWithImage;
} catch (err) {
logger.error(
`Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}`,
err.message,
);
throw new CustomError(
`Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}`,
`Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down
34 changes: 20 additions & 14 deletions src/service/keyword.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ async function createKeyword(info: IKeywordCreatePayload): Promise<IKeywordDocum

return newKeywordObj;
} catch (err) {
logger.error(`Failed to create keyword ${info.name}: ${err.message}`);
throw new CustomError('Failed to create keyword', HttpCode.INTERNAL_SERVER_ERROR);
throw new CustomError(
`Failed to create keyword ${info.name}: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}

Expand All @@ -47,6 +49,7 @@ async function updateKeyword(
logger.info(`Update keyword - Keyword(${keywordId})`);
return updatedKeyword;
}

async function deleteKeyword(keywordId: Types.ObjectId): Promise<boolean> {
const deletedKeyword = await KeywordModel.findOneAndDelete({ _id: keywordId }).lean();
if (_.isNull(deletedKeyword)) {
Expand All @@ -63,8 +66,10 @@ async function getTopKeywords(limit: number = 6): Promise<IKeywordDocument[]> {
.lean();
return topKeywords;
} catch (err) {
logger.error(`Failed to get top keywords: ${err.message}`);
throw new CustomError('Failed to get top keywords', HttpCode.INTERNAL_SERVER_ERROR);
throw new CustomError(
`Failed to get top keywords: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}

Expand All @@ -80,8 +85,10 @@ async function increaseSearchCount(keywordId: Types.ObjectId): Promise<IKeywordD
}
return updatedKeyword;
} catch (err) {
logger.error(`Failed to increase searchCount for keywordId ${keywordId}: ${err.message}`);
throw new CustomError('Failed to increase searchCount', HttpCode.INTERNAL_SERVER_ERROR);
throw new CustomError(
`Failed to increase searchCount for keywordId ${keywordId}: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}

Expand All @@ -90,9 +97,8 @@ async function getKeywordByName(keywordName: string): Promise<IKeywordDocument |
const keyword = await KeywordModel.findOne({ name: keywordName, isDeleted: false }).lean();
return keyword || null;
} catch (err) {
logger.error(`Failed to get a Keyword Info By Name(${keywordName})`);
throw new CustomError(
`Failed to get a Keyword Info By Name(${keywordName}) (${err.message})`,
`Failed to get a Keyword Info By Name(${keywordName}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -103,9 +109,8 @@ async function getKeywordById(keywordId: Types.ObjectId): Promise<IKeywordDocume
const keyword = await KeywordModel.findOne({ _id: keywordId, isDeleted: false }).lean();
return keyword || null;
} catch (err) {
logger.info(`Failed to get a Keyword Info By id (${keywordId})`);
throw new CustomError(
`Failed to get a Keyword Info By id(${keywordId}) (${err.message})`,
`Failed to get a Keyword Info By id(${keywordId}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -124,9 +129,8 @@ async function getKeywordInfoByKeywordIds(
).lean();
return keyword;
} catch (err) {
logger.error(`Failed to get a Keyword Info By keywordIds(${JSON.stringify(keywordIds)})`);
throw new CustomError(
`Failed to get a Keyword Info By keywordIds(${JSON.stringify(keywordIds)})`,
`Failed to get a Keyword Info By keywordIds(${JSON.stringify(keywordIds)}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -172,8 +176,10 @@ async function getRecommendedKeywords(): Promise<
logger.info('Successfully retrieved recommended keywords');
return result;
} catch (err) {
logger.error(`Failed to get recommended keywords: ${err.message}`);
throw new CustomError('Failed to get recommended keywords', HttpCode.INTERNAL_SERVER_ERROR);
throw new CustomError(
`Failed to get recommended keywords: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/service/keywordCategory.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ async function createKeywordCategory(
logger.info(`Created new keyword category: ${JSON.stringify(newCategoryObj)}`);
return newCategoryObj;
} catch (err) {
logger.error(`Failed to create category ${info.name}: ${err.message}`);
throw new CustomError('Failed to create category', HttpCode.INTERNAL_SERVER_ERROR);
throw new CustomError(
`Failed to create category ${info.name}: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}

Expand Down Expand Up @@ -55,6 +57,7 @@ async function deleteKeywordCategory(categoryName: string): Promise<boolean> {
if (_.isNull(deletedCategory)) {
throw new CustomError(`Category with Name ${categoryName} not found`, HttpCode.NOT_FOUND);
}

return true;
}

Expand Down
28 changes: 11 additions & 17 deletions src/service/meme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ async function getMeme(memeId: string): Promise<IMemeDocument | null> {

return meme || null;
} catch (err) {
logger.error(`Failed to get a meme(${memeId}): ${err.message}`);
throw new CustomError(`Failed to get a meme(${memeId})`, HttpCode.INTERNAL_SERVER_ERROR);
throw new CustomError(
`Failed to get a meme(${memeId}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}

Expand Down Expand Up @@ -51,9 +53,8 @@ async function getMemeWithKeywords(
isReaction: !_.isNil(isReaction),
};
} catch (err) {
logger.error(`Failed to get a meme(${meme._id}) with keywords: ${err.message}`);
throw new CustomError(
`Failed to get a meme(${meme._id}) with keywords`,
`Failed to get a meme(${meme._id}) with keywords: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -78,9 +79,8 @@ async function getTodayMemeList(

return memeList;
} catch (err) {
logger.error(`Failed to get today meme list: ${err.message}`);
throw new CustomError(
`Failed to get today meme list ${err.message}`,
`Failed to get today meme list: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -141,9 +141,8 @@ async function getMemeListWithKeywordsAndisSavedAndisReaction(
}),
);
} catch (err) {
logger.error('Failed to get keywords and isSaved info from meme list', err.message);
throw new CustomError(
`Failed to get keywords and isSaved info from meme list ${err.message}`,
`Failed to get keywords and isSaved info from meme list: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -231,9 +230,8 @@ async function searchMemeByKeyword(
data: memeList,
};
} catch (err) {
logger.error(`Failed to search meme list with keyword(${keyword})`, err.message);
throw new CustomError(
`Failed to search meme list with keyword(${keyword})`,
`Failed to search meme list with keyword(${keyword}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -269,10 +267,8 @@ async function createMemeInteraction(
}
return true;
} catch (err) {
logger.error(`Failed to create memeInteraction(${interactionType})`, err.message);

throw new CustomError(
`Failed to create memeInteraction(${interactionType}) (${err.message})`,
`Failed to create memeInteraction(${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -283,9 +279,8 @@ async function deleteMemeSave(user: IUserDocument, meme: IMemeDocument): Promise
await MemeInteractionService.deleteMemeInteraction(user, meme, InteractionType.SAVE);
return true;
} catch (err) {
logger.error(`Failed to delete meme save`, err.message);
throw new CustomError(
`Failed to delete meme save(${err.message})`,
`Failed to delete meme save: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -307,9 +302,8 @@ async function getTopReactionImage(keyword: IKeywordDocument): Promise<string> {
logger.info(`Get top reaction meme - keyword(${keyword.name}), meme(${topReactionMeme._id})`);
return topReactionMeme.image;
} catch (err) {
logger.error(`Failed get top reaction meme image`, err.message);
throw new CustomError(
`Failed get top reaction meme(${err.message})`,
`Failed get top reaction meme image: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down
19 changes: 6 additions & 13 deletions src/service/memeInteraction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ async function getMemeInteractionInfo(

return memeInteraction || null;
} catch (err) {
logger.error(`Failed to get a MemeInteraction info(${meme._id} - ${interactionType})`);
throw new CustomError(
`Failed to get a MemeInteraction info(${meme._id} - ${interactionType})`,
`Failed to get a MemeInteraction info(${meme._id} - ${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -58,9 +57,8 @@ async function getMemeInteractionInfoWithCondition(
const memeInteraction = await MemeInteractionModel.findOne(condition);
return memeInteraction || null;
} catch (err) {
logger.error(`Failed to get a MemeInteraction Info(${meme._id} - ${interactionType})`);
throw new CustomError(
`Failed to get a MemeInteraction Info(${meme._id} - ${interactionType})`,
`Failed to get a MemeInteraction Info(${meme._id} - ${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -78,9 +76,8 @@ async function getMemeInteractionCount(
});
return count;
} catch (err) {
logger.error(`Failed to count MemeInteraction(${interactionType})`);
throw new CustomError(
`Failed to count MemeInteraction(${interactionType}) (${err.message})`,
`Failed to count MemeInteraction(${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -107,9 +104,8 @@ async function getMemeInteractionList(

return memeInteractionList;
} catch (err) {
logger.error(`Failed to count MemeInteraction(${interactionType})`);
throw new CustomError(
`Failed to count MemeInteraction(${interactionType}) (${err.message})`,
`Failed to count MemeInteraction(${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand All @@ -129,9 +125,8 @@ async function createMemeInteraction(
await newMemeInteraction.save();
return newMemeInteraction;
} catch (err) {
logger.error(`Failed to create a MemeInteraction(${meme._id} - ${interactionType})`);
throw new CustomError(
`Failed to create a MemeInteraction(${meme._id} - ${interactionType})`,
`Failed to create a MemeInteraction(${meme._id} - ${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -173,7 +168,6 @@ async function updateMemeInteraction(
break;

default:
logger.error(`Unsupported interactionType(${interactionType})`);
throw new CustomError(
`Unsupported interactionType(${interactionType})`,
HttpCode.BAD_REQUEST,
Expand All @@ -196,9 +190,8 @@ async function deleteMemeInteraction(

return memeInteraction;
} catch (err) {
logger.error(`Failed to delete a MemeInteraction(${meme._id} - ${interactionType})`);
throw new CustomError(
`Failed to delete a MemeInteraction(${meme._id} - ${interactionType})`,
`Failed to delete a MemeInteraction(${meme._id} - ${interactionType}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down
13 changes: 4 additions & 9 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ async function getUser(deviceId: string): Promise<IUserDocument | null> {
);
return user?.toObject() || null;
} catch (err) {
logger.error(`Failed to getUser - deviceId${deviceId}`);
throw new CustomError(
'Failed to getUser - deviceId${deviceId}`',
`Failed to getUser - deviceId(${deviceId}): ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -88,7 +87,6 @@ async function createUser(deviceId: string): Promise<IUserInfos> {
logger.info(`Created user - deviceId(${JSON.stringify(user.toObject())})`);
return { ...user.toObject(), watch: 0, share: 0, reaction: 0, save: 0 };
} catch (err) {
logger.error(`Failed to create User`);
throw new CustomError(`Failed to create a User`, HttpCode.INTERNAL_SERVER_ERROR);
}
}
Expand Down Expand Up @@ -126,9 +124,8 @@ async function updateLastSeenMeme(user: IUserDocument, meme: IMemeDocument): Pro
);
return updatedUser;
} catch (err) {
logger.error(`Failed Update user lastSeenMeme`, err.message);
throw new CustomError(
`Failed Update user lastSeenMeme(${err.message})`,
`Failed Update user lastSeenMeme: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -161,9 +158,8 @@ async function getLastSeenMemeList(user: IUserDocument): Promise<IMemeGetRespons
const sortedGetLastSeenMemeList = lastSeenMeme.map((id) => memeMap[id.toString()]);
return sortedGetLastSeenMemeList;
} catch (err) {
logger.error(`Failed get lastSeenMemeList`, err.message);
throw new CustomError(
`Failed get lastSeenMemeList(${err.message})`,
`Failed to get lastSeenMemeList: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down Expand Up @@ -248,9 +244,8 @@ async function createMemeRecommendWatch(user: IUserDocument, meme: IMemeDocument

return memeRecommendWatchCount;
} catch (err) {
logger.error(`Failed create memeRecommendWatch`, err.message);
throw new CustomError(
`Failed create memeRecommendWatch(${err.message})`,
`Failed to create memeRecommendWatch(${err.message})`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down

0 comments on commit 14b95c2

Please sign in to comment.