Skip to content

Commit

Permalink
hacker error logging and error object change
Browse files Browse the repository at this point in the history
  • Loading branch information
donghquinn committed Mar 4, 2024
1 parent bd33fae commit 96b4bc8
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 122 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ npm-debug.log
example.env

migrations
.eslintrc.json

newsData
1 change: 0 additions & 1 deletion src/controllers/client/client.ctl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export class ClientController {

const encodedEmail = await this.client.login(email, password);


return new SetResponse(200, { email: encodedEmail });
} catch (error) {
return new SetErrorResponse(error);
Expand Down
8 changes: 4 additions & 4 deletions src/controllers/geek/geek.ctl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Post, Query } from '@nestjs/common';
import { hadaNewsStarValidator, hadaNewsUnStarValidator, hadaNewsValidator } from '@validators/hada.validator';
import { geekNewsStarValidator, geekNewsUnStarValidator, geekNewsValidator } from '@validators/geek.validator';
import { SetErrorResponse, SetResponse } from 'dto/response.dto';
import { GeekProvider } from 'providers/news/geek/geek.pvd';
import { DailyHadaNewsRequest } from 'types/geek.type';
Expand All @@ -16,7 +16,7 @@ export class GeekController {
@Query('size') size: number,
) {
try {
const { today } = await hadaNewsValidator(request);
const { today } = await geekNewsValidator(request);

const { result, total } = await this.geek.getNews(today, page, size);

Expand All @@ -29,7 +29,7 @@ export class GeekController {
@Post('star')
async geekGiveStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await hadaNewsStarValidator(request);
const { uuid: postUuid, email } = await geekNewsStarValidator(request);

await this.geek.giveStar(postUuid, email);

Expand All @@ -42,7 +42,7 @@ export class GeekController {
@Post('unstar')
async geekUnStarController(@Body() request: StarRequest) {
try {
const { uuid: postUuid, email } = await hadaNewsUnStarValidator(request);
const { uuid: postUuid, email } = await geekNewsUnStarValidator(request);

await this.geek.unStar(postUuid, email);

Expand Down
2 changes: 1 addition & 1 deletion src/dto/response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuthError } from '@errors/auth.error';
import { ClientError } from '@errors/client.error';
import { HackerError } from '@errors/hacker.error';
import { HadaError } from '@errors/hada.error';
import { HadaError } from '@errors/geek.error';
import { MachineLearningError } from '@errors/machine.error';
import { PrismaError } from '@errors/prisma.error';
import { ValidatorError } from '@errors/validator.error';
Expand Down
4 changes: 2 additions & 2 deletions src/errors/hada.error.ts → src/errors/geek.error.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export class HadaError extends Error {
export class GeekError extends Error {
type: string;

constructor(type: string, message: string, cause?: Error) {
super(message);

this.type = type;

this.name = '[Hada Error]';
this.name = '[Geek Error]';

this.cause = cause;
}
Expand Down
96 changes: 26 additions & 70 deletions src/providers/news/geek/geek-prisma.lib.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { HadaError } from '@errors/hada.error';
import { PrismaError } from '@errors/prisma.error';
import { GeekError } from '@errors/geek.error';
import { Injectable } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { NewsLogger } from '@utils/logger.util';

@Injectable()
export class GeekPrismaLibrary extends PrismaClient {
async bringHadaNews(startDate: Date, endDate: Date, page: number, size: number) {
async bringGeekNews(startDate: Date, endDate: Date, page: number, size: number) {
try {
const result = await this.geek.findMany({
select: { uuid: true, post: true, link: true, descLink: true, founded: true, liked: true },
Expand All @@ -23,12 +22,12 @@ export class GeekPrismaLibrary extends PrismaClient {

return result;
} catch (error) {
NewsLogger.error('[HADA] Bring Geek News Error: %o', {
NewsLogger.error('[GEEK] Bring Geek News Error: %o', {
error,
});

throw new PrismaError(
'[HADA] Bring Geek News',
throw new GeekError(
'[GEEK] Bring Geek News',
'Bring Geek News Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
Expand All @@ -48,19 +47,19 @@ export class GeekPrismaLibrary extends PrismaClient {

return Math.ceil(totalCounts / size);
} catch (error) {
NewsLogger.error('[HADA] Bring Total Geek News Count Error: %o', {
NewsLogger.error('[GEEK] Bring Total Geek News Count Error: %o', {
error,
});

throw new PrismaError(
'[HADA] Bring Total Geek News Count',
throw new GeekError(
'[GEEK] Bring Total Geek News Count',
'Bring Total Geek News Count Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async checkHadaNewsIsLiked(postUuid: string, clientUuid: string) {
async checkGeekNewsIsLiked(postUuid: string, clientUuid: string) {
try {
const isStarred = await this.geek_Liked.findFirst({
select: {
Expand All @@ -77,29 +76,29 @@ export class GeekPrismaLibrary extends PrismaClient {
},
});

if (isStarred === null) throw new HadaError('[Hada] Get Star Info', 'No Star Info Found.');
if (isStarred === null) throw new GeekError('[GEEK] Get Star Info', 'No Star Info Found.');

NewsLogger.debug('[Hada] Found Is Starred Info: %o', {
NewsLogger.debug('[GEEK] Found Is Starred Info: %o', {
isLiked: isStarred.geek_news.liked,
});

return { uuid: isStarred.uuid, liked: isStarred.geek_news.liked };
} catch (error) {
NewsLogger.error('[Hada] Check Hada News Liked Info Error: %o', {
NewsLogger.error('[GEEK] Check Hada News Liked Info Error: %o', {
error,
});

throw new HadaError(
'[Hada] Check Hada News Liked Info',
throw new GeekError(
'[GEEK] Check Hada News Liked Info',
'Check Hada News Liked Info Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async updateHadaNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string) {
async updateGeekNewsLikedtoUnliked(likedUuid: string, postUuid: string, clientUuid: string) {
try {
NewsLogger.debug('[HADA] Give Hada News unStar Request: %o', {
NewsLogger.debug('[GEEK] Give Hada News unStar Request: %o', {
likedUuid,
postUuid,
clientUuid,
Expand All @@ -120,25 +119,25 @@ export class GeekPrismaLibrary extends PrismaClient {
},
});

NewsLogger.info('[HADA] Unstarred Updated');
NewsLogger.info('[GEEK] Unstarred Updated');

return 0;
} catch (error) {
NewsLogger.error('[Hada] Update Liked to UnLiked Error: %o', {
NewsLogger.error('[GEEK] Update Liked to UnLiked Error: %o', {
error,
});

throw new HadaError(
'[Hada] Update Liked to UnLiked',
throw new GeekError(
'[GEEK] Update Liked to UnLiked',
'Update Liked to UnLiked Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async updateHadaNewsLiked(likedUuid: string, postUuid: string, clientUuid: string) {
async updateGeekNewsLiked(likedUuid: string, postUuid: string, clientUuid: string) {
try {
NewsLogger.debug('[HADA] Give Hacker News Star Request: %o', {
NewsLogger.debug('[GEEK] Give Hacker News Star Request: %o', {
likedUuid,
postUuid,
clientUuid,
Expand All @@ -159,62 +158,19 @@ export class GeekPrismaLibrary extends PrismaClient {
},
});

NewsLogger.info('[HADA] Starred Updated');
NewsLogger.info('[GEEK] Starred Updated');

return 0;
} catch (error) {
NewsLogger.error('[Hada] Update News Liked Error: %o', {
NewsLogger.error('[GEEK] Update News Liked Error: %o', {
error,
});

throw new HadaError(
'[Hada] Update News Liked',
throw new GeekError(
'[GEEK] Update News Liked',
'Update News Liked Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}

async getStarredHadaNewsPagination(page: number, size: number) {
try {
const totalPosts = await this.geek.count({ where: { liked: 1 } });

const starredNews = await this.geek.findMany({
select: {
uuid: true,
post: true,
link: true,
founded: true,
},
orderBy: {
founded: 'desc',
},
where: {
liked: 1,
},
take: size,
skip: (page - 1) * size,
});

NewsLogger.debug('[Hada] Founded Starred News: %o', {
totalPosts,
newsSize: starredNews.length,
});

return {
totalPosts,
starredNews,
};
} catch (error) {
NewsLogger.info('[Hada] Get Starred News Error: %o', {
error,
});

throw new HadaError(
'[Hada] Get Starred News',
'Get Starred News Error.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
);
}
}
}
22 changes: 11 additions & 11 deletions src/providers/news/geek/geek.pvd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HadaError } from '@errors/hada.error';
import { GeekError } from '@errors/geek.error';
import { Injectable } from '@nestjs/common';
import { NewsLogger } from '@utils/logger.util';
import { endOfDay, startOfDay } from 'date-fns';
Expand Down Expand Up @@ -32,7 +32,7 @@ export class GeekProvider {
size,
});

const result = await this.prisma.bringHadaNews(startDate, endDate, page, size);
const result = await this.prisma.bringGeekNews(startDate, endDate, page, size);

for (let i = 0; i <= result.length - 1; i += 1) {
const isUrlUndefined = result[i].descLink.split('.io/')[1];
Expand Down Expand Up @@ -80,7 +80,7 @@ export class GeekProvider {
error,
});

throw new HadaError(
throw new GeekError(
'[GEEK] Bring news',
'Bring Hada News Error. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
Expand All @@ -92,20 +92,20 @@ export class GeekProvider {
try {
const isLogined = await this.account.getItem(email);

if (isLogined === null) throw new HadaError('[GEEK] Give Star on the Stars', 'No Logined User Found.');
if (isLogined === null) throw new GeekError('[GEEK] Give Star on the Stars', 'No Logined User Found.');

const { uuid: clientUuid } = isLogined;
const { uuid: likedUuid, liked } = await this.prisma.checkHadaNewsIsLiked(postUuid, clientUuid);
const { uuid: likedUuid, liked } = await this.prisma.checkGeekNewsIsLiked(postUuid, clientUuid);

if (!liked) {
await this.prisma.updateHadaNewsLiked(likedUuid, postUuid, clientUuid);
await this.prisma.updateGeekNewsLiked(likedUuid, postUuid, clientUuid);
}
} catch (error) {
NewsLogger.error('[GEEK] Star Update Error: %o', {
error,
});

throw new HadaError(
throw new GeekError(
'[GEEK] Give Star on the news',
'Failed to vie star news',
error instanceof Error ? error : new Error(JSON.stringify(error)),
Expand All @@ -117,20 +117,20 @@ export class GeekProvider {
try {
const isLogined = await this.account.getItem(email);

if (isLogined === null) throw new HadaError('[GEEK] Give Star on the Stars', 'No Logined User Found.');
if (isLogined === null) throw new GeekError('[GEEK] Give Star on the Stars', 'No Logined User Found.');

const { uuid: clientUuid } = isLogined;
const { uuid: likedUuid, liked } = await this.prisma.checkHadaNewsIsLiked(postUuid, clientUuid);
const { uuid: likedUuid, liked } = await this.prisma.checkGeekNewsIsLiked(postUuid, clientUuid);

if (liked) await this.prisma.updateHadaNewsLikedtoUnliked(likedUuid, postUuid, clientUuid);
if (liked) await this.prisma.updateGeekNewsLikedtoUnliked(likedUuid, postUuid, clientUuid);

NewsLogger.info('[GEEK] Finished UnStar Geek News');
} catch (error) {
NewsLogger.error('[GEEK] Finished UnStar Geek News Error: %o', {
error,
});

throw new HadaError(
throw new GeekError(
'[GEEK] Finished UnStar Geek News',
'Failed to Finished UnStar Geek News. Please Try Again.',
error instanceof Error ? error : new Error(JSON.stringify(error)),
Expand Down
Loading

0 comments on commit 96b4bc8

Please sign in to comment.