From c4771dde32f52da87b3ec49e47cd59f824dfa418 Mon Sep 17 00:00:00 2001 From: seohyun0120 Date: Sat, 21 Sep 2024 16:22:47 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20log=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/keyword.controller.ts | 9 ++++++--- src/controller/keywordCategory.controller.ts | 4 ++-- src/controller/meme.controller.ts | 2 +- src/middleware/requestedInfo.ts | 2 +- src/service/keyword.service.ts | 8 ++++---- src/service/memeInteraction.service.ts | 8 +++----- src/service/user.service.ts | 4 ++-- 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/controller/keyword.controller.ts b/src/controller/keyword.controller.ts index 2a883d4..2a96917 100644 --- a/src/controller/keyword.controller.ts +++ b/src/controller/keyword.controller.ts @@ -46,12 +46,15 @@ const getTopKeywords = async (req: Request, res: Response, next: NextFunction) = try { const topReactionImage: string = await MemeService.getTopReactionImage(keyword); return { ...keyword, topReactionImage } as IKeywordWithImage; - } catch (error) { + } catch (err) { logger.error( `Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}`, - error, + err.message, + ); + throw new CustomError( + `Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}`, + HttpCode.INTERNAL_SERVER_ERROR, ); - throw new CustomError(`Failed to get top reaction image`, HttpCode.INTERNAL_SERVER_ERROR); } }, ); diff --git a/src/controller/keywordCategory.controller.ts b/src/controller/keywordCategory.controller.ts index 9b27e88..11b4df4 100644 --- a/src/controller/keywordCategory.controller.ts +++ b/src/controller/keywordCategory.controller.ts @@ -30,7 +30,7 @@ const updateKeywordCategory = async (req: CustomRequest, res: Response, next: Ne updateInfo, ); logger.info(`Updated category with ID ${req.params.categoryName}`); - return res.json(createSuccessResponse(HttpCode.OK, 'Update KeywordCategor', updatedCategory)); + return res.json(createSuccessResponse(HttpCode.OK, 'Updated KeywordCategory', updatedCategory)); } catch (err) { return next(new CustomError(err.message, err.status || HttpCode.INTERNAL_SERVER_ERROR)); } @@ -40,7 +40,7 @@ const deleteKeywordCategory = async (req: CustomRequest, res: Response, next: Ne const category = req.requestedKeywordCategory; try { await KeywordCategoryService.deleteKeywordCategory(category.name); - return res.json(createSuccessResponse(HttpCode.OK, 'Deleted KeywordCategor', true)); + return res.json(createSuccessResponse(HttpCode.OK, 'Deleted KeywordCategory', true)); } catch (err) { return next(new CustomError(err.message, err.status || HttpCode.INTERNAL_SERVER_ERROR)); } diff --git a/src/controller/meme.controller.ts b/src/controller/meme.controller.ts index 7f37703..ab10c2b 100644 --- a/src/controller/meme.controller.ts +++ b/src/controller/meme.controller.ts @@ -24,7 +24,7 @@ const getMeme = async (req: Request, res: Response, next: NextFunction) => { } if (!mongoose.Types.ObjectId.isValid(memeId)) { - return next(new CustomError(`'memeId' is not a valid ObjectId`, HttpCode.BAD_REQUEST)); + return next(new CustomError(`${memeId} is not a valid ObjectId`, HttpCode.BAD_REQUEST)); } try { diff --git a/src/middleware/requestedInfo.ts b/src/middleware/requestedInfo.ts index 9b776e5..a7342ef 100644 --- a/src/middleware/requestedInfo.ts +++ b/src/middleware/requestedInfo.ts @@ -35,7 +35,7 @@ export const getRequestedMemeInfo = async ( } if (!mongoose.Types.ObjectId.isValid(memeId)) { - return next(new CustomError(`'memeId' is not a valid ObjectId`, HttpCode.BAD_REQUEST)); + return next(new CustomError(`${memeId} is not a valid ObjectId`, HttpCode.BAD_REQUEST)); } const meme: IMemeDocument = await getMeme(memeId); diff --git a/src/service/keyword.service.ts b/src/service/keyword.service.ts index d2eca81..303f226 100644 --- a/src/service/keyword.service.ts +++ b/src/service/keyword.service.ts @@ -41,16 +41,16 @@ async function updateKeyword( ).lean(); if (_.isNull(updatedKeyword)) { - throw new CustomError(`Keyword with ID ${keywordId} not found`, HttpCode.NOT_FOUND); + throw new CustomError(`Keyword(${keywordId}) not found`, HttpCode.NOT_FOUND); } - logger.info(`Update keyword - keyword(${keywordId})`); + logger.info(`Update keyword - Keyword(${keywordId})`); return updatedKeyword; } async function deleteKeyword(keywordId: Types.ObjectId): Promise { const deletedKeyword = await KeywordModel.findOneAndDelete({ _id: keywordId }).lean(); if (_.isNull(deletedKeyword)) { - throw new CustomError(`Keyword with ID ${keywordId} not found`, HttpCode.NOT_FOUND); + throw new CustomError(`Keyword(${keywordId}) not found`, HttpCode.NOT_FOUND); } return true; } @@ -76,7 +76,7 @@ async function increaseSearchCount(keywordId: Types.ObjectId): Promise Date: Sat, 21 Sep 2024 16:50:06 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20logger.error=20=EB=A1=9C=EA=B7=B8=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/keyword.controller.ts | 6 +---- src/service/keyword.service.ts | 34 +++++++++++++++----------- src/service/keywordCategory.service.ts | 7 ++++-- src/service/meme.service.ts | 28 +++++++++------------ src/service/memeInteraction.service.ts | 19 +++++--------- src/service/user.service.ts | 13 +++------- 6 files changed, 47 insertions(+), 60 deletions(-) diff --git a/src/controller/keyword.controller.ts b/src/controller/keyword.controller.ts index 2a96917..3cd8d6a 100644 --- a/src/controller/keyword.controller.ts +++ b/src/controller/keyword.controller.ts @@ -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, ); } diff --git a/src/service/keyword.service.ts b/src/service/keyword.service.ts index 303f226..5923a75 100644 --- a/src/service/keyword.service.ts +++ b/src/service/keyword.service.ts @@ -25,8 +25,10 @@ async function createKeyword(info: IKeywordCreatePayload): Promise { const deletedKeyword = await KeywordModel.findOneAndDelete({ _id: keywordId }).lean(); if (_.isNull(deletedKeyword)) { @@ -63,8 +66,10 @@ async function getTopKeywords(limit: number = 6): Promise { .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, + ); } } @@ -80,8 +85,10 @@ async function increaseSearchCount(keywordId: Types.ObjectId): Promise { if (_.isNull(deletedCategory)) { throw new CustomError(`Category with Name ${categoryName} not found`, HttpCode.NOT_FOUND); } + return true; } diff --git a/src/service/meme.service.ts b/src/service/meme.service.ts index 10ae218..7513dbe 100644 --- a/src/service/meme.service.ts +++ b/src/service/meme.service.ts @@ -19,8 +19,10 @@ async function getMeme(memeId: string): Promise { 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, + ); } } @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -307,9 +302,8 @@ async function getTopReactionImage(keyword: IKeywordDocument): Promise { 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, ); } diff --git a/src/service/memeInteraction.service.ts b/src/service/memeInteraction.service.ts index 29590d6..522a4e9 100644 --- a/src/service/memeInteraction.service.ts +++ b/src/service/memeInteraction.service.ts @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -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, ); } @@ -173,7 +168,6 @@ async function updateMemeInteraction( break; default: - logger.error(`Unsupported interactionType(${interactionType})`); throw new CustomError( `Unsupported interactionType(${interactionType})`, HttpCode.BAD_REQUEST, @@ -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, ); } diff --git a/src/service/user.service.ts b/src/service/user.service.ts index 1031abc..a6c7f54 100644 --- a/src/service/user.service.ts +++ b/src/service/user.service.ts @@ -26,9 +26,8 @@ async function getUser(deviceId: string): Promise { ); 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, ); } @@ -88,7 +87,6 @@ async function createUser(deviceId: string): Promise { 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); } } @@ -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, ); } @@ -161,9 +158,8 @@ async function getLastSeenMemeList(user: IUserDocument): Promise 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, ); } @@ -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, ); }