diff --git a/apps/comments-service/src/comments-service.service.ts b/apps/comments-service/src/comments-service.service.ts index aa07c224e..bbdb855bf 100644 --- a/apps/comments-service/src/comments-service.service.ts +++ b/apps/comments-service/src/comments-service.service.ts @@ -1,5 +1,5 @@ -import { HttpStatus, Inject, Injectable } from '@nestjs/common'; -import { ClientProxy, RpcException } from '@nestjs/microservices'; +import { Inject, Injectable } from '@nestjs/common'; +import { ClientProxy } from '@nestjs/microservices'; import { ModelType } from '@typegoose/typegoose/lib/types'; import { PaginateModel } from 'mongoose'; import { nextTick } from 'process'; @@ -7,6 +7,8 @@ import { InjectModel } from '~/libs/database/src/model.transformer'; import { ExceptionMessage } from '~/shared/constants/echo.constant'; import { NotificationEvents } from '~/shared/constants/event.constant'; import { ServicesEnum } from '~/shared/constants/services.constant'; +import { BadRequestRpcExcption } from '~/shared/exceptions/bad-request-rpc-exception'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; import { CommentReactions, CommentsModel, @@ -22,7 +24,7 @@ export class CommentsService { @Inject(ServicesEnum.notification) private readonly notification: ClientProxy, - ) {} + ) { } get model() { return this.CommentsModel; @@ -97,10 +99,7 @@ export class CommentsService { async deleteComment(id: string) { const comment = await this.CommentsModel.findOneAndDelete({ id }); if (!comment) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.CommentNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.CommentNotFound); } const { parent, children } = comment; if (children && children.length) { @@ -142,10 +141,7 @@ export class CommentsService { async replyComment(parent: string, data: CommentsModel, isMaster: boolean) { const parentComment = await this.CommentsModel.findById(parent); if (!parentComment) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.CommentNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.CommentNotFound); } data.pid = parentComment.pid; // 防止恶意修改,强制使用父评论的pid if (isMaster) { @@ -183,17 +179,11 @@ export class CommentsService { ) { const comment = this.CommentsModel.findById(id); if (!comment) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.CommentNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.CommentNotFound); } // 验证 reaction 是否合法 if (!Object.values(CommentReactions).includes(reaction)) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.InvalidCommentReaction, - }); + throw new BadRequestRpcExcption(ExceptionMessage.InvalidCommentReaction); } const update = isAdd ? { $inc: { [`reaction.${reaction}`]: 1 } } diff --git a/apps/friends-service/src/friends-service.service.ts b/apps/friends-service/src/friends-service.service.ts index 82d08eeb4..c81a13e4f 100644 --- a/apps/friends-service/src/friends-service.service.ts +++ b/apps/friends-service/src/friends-service.service.ts @@ -1,4 +1,4 @@ -import { HttpStatus, Inject, Injectable, Logger } from '@nestjs/common'; +import { Inject, Injectable, Logger } from '@nestjs/common'; import { ReturnModelType } from '@typegoose/typegoose'; import { v4 } from 'uuid'; import { HttpService } from '~/libs/helper/src/helper.http.service'; @@ -6,13 +6,16 @@ import { InjectModel } from '~/shared/transformers/model.transformer'; import { FriendsModel, FriendStatus } from './friends.model'; import { JSDOM } from 'jsdom'; import { ConfigService } from '~/libs/config/src'; -import { ClientProxy, RpcException } from '@nestjs/microservices'; +import { ClientProxy } from '@nestjs/microservices'; import { ExceptionMessage } from '~/shared/constants/echo.constant'; import { nextTick } from 'process'; import { FeedParser } from '~/shared/utils'; import { isValidObjectId } from 'mongoose'; import { ServicesEnum } from '~/shared/constants/services.constant'; import { NotificationEvents } from '~/shared/constants/event.constant'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; +import { BadRequestRpcExcption } from '~/shared/exceptions/bad-request-rpc-exception'; +import { UnauthorizedRpcExcption } from '~/shared/exceptions/unauthorized-rpc-exception'; @Injectable() export class FriendsService { constructor( @@ -23,24 +26,18 @@ export class FriendsService { @Inject(ServicesEnum.notification) private readonly notification: ClientProxy, - ) {} + ) { } public get model() { return this.friendsModel; } private throwInvalidTokenException(): never { - throw new RpcException({ - code: HttpStatus.UNAUTHORIZED, - message: ExceptionMessage.FriendLinkTokenIsInvalid, - }); + throw new UnauthorizedRpcExcption(ExceptionMessage.FriendLinkTokenIsInvalid); } private throwNotFoundException(): never { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.FriendLinkIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.FriendLinkIsNotExist); } private async checkAlive(url: string) { @@ -148,10 +145,7 @@ export class FriendsService { item?.status !== FriendStatus.Trash, ) ) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.FriendLinkIsExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.FriendLinkIsExist); } if (res.some((item) => item?.status === FriendStatus.Trash)) { const friend = res.find((item) => item?.status === FriendStatus.Trash); @@ -172,8 +166,7 @@ export class FriendsService { friend.autoCheck = await this.autoCheck(input.data.link); await friend.save(); Logger.warn( - `${friend.name} 申请友链 - 互链检测: ${ - friend.autoCheck ? '通过' : '未通过' + `${friend.name} 申请友链 - 互链检测: ${friend.autoCheck ? '通过' : '未通过' }`, FriendsService.name, ); @@ -225,8 +218,7 @@ export class FriendsService { friend.autoCheck = await this.autoCheck(data.link); // 重新检测 await friend.save(); Logger.warn( - `${friend.name} 申请友链 - 互链检测: ${ - friend.autoCheck ? '通过' : '未通过' + `${friend.name} 申请友链 - 互链检测: ${friend.autoCheck ? '通过' : '未通过' }`, FriendsService.name, ); @@ -292,10 +284,7 @@ export class FriendsService { async analyseAutoCheck(id: string) { const friend = await this.friendsModel.findById(id); if (!friend) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.FriendLinkIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.FriendLinkIsNotExist); } const isAlive = await this.checkAlive(friend.link); if (!isAlive) { diff --git a/apps/page-service/src/category.service.ts b/apps/page-service/src/category.service.ts index b01de632b..9e3c1cd09 100644 --- a/apps/page-service/src/category.service.ts +++ b/apps/page-service/src/category.service.ts @@ -6,13 +6,14 @@ * @LastEditTime: 2022-09-24 15:36:36 * Coding With IU */ -import { HttpStatus, Inject, Injectable } from '@nestjs/common'; -import { RpcException } from '@nestjs/microservices'; +import { Inject, Injectable } from '@nestjs/common'; import { ReturnModelType, DocumentType } from '@typegoose/typegoose'; import { FilterQuery } from 'mongoose'; import { nextTick } from 'process'; import { InjectModel } from '~/libs/database/src/model.transformer'; import { ExceptionMessage } from '~/shared/constants/echo.constant'; +import { BadRequestRpcExcption } from '~/shared/exceptions/bad-request-rpc-exception'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; import { MultiCategoriesQueryDto } from './dto/category.dto'; import { CategoryModel, CategoryType } from './model/category.model'; import { PostModel } from './model/post.model'; @@ -157,10 +158,7 @@ export class CategoryService { .populate('category'); if (!posts.length) - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PageIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PageIsNotExist); return posts; } @@ -234,17 +232,11 @@ export class CategoryService { async deleteCategory(categoryId: string) { const category = await this.model.findById(categoryId); if (!category) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.CategoryIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.CategoryIsNotExist); } const postsInCategory = await this.findPostsInCategory(category.id); if (postsInCategory.length > 0) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.CategoryHasPost, - }); + throw new BadRequestRpcExcption(ExceptionMessage.CategoryHasPost); } const action = this.categoryModel.deleteOne({ _id: categoryId, diff --git a/apps/page-service/src/page-service.controller.ts b/apps/page-service/src/page-service.controller.ts index ae9029b0e..8a4875037 100644 --- a/apps/page-service/src/page-service.controller.ts +++ b/apps/page-service/src/page-service.controller.ts @@ -1,6 +1,6 @@ import slugify from 'slugify'; -import { BadRequestException, Controller, HttpStatus } from '@nestjs/common'; -import { MessagePattern, RpcException } from '@nestjs/microservices'; +import { BadRequestException, Controller } from '@nestjs/common'; +import { MessagePattern } from '@nestjs/microservices'; import { ApiOperation, ApiParam, ApiQuery } from '@nestjs/swagger'; import { CategoryEvents, @@ -19,6 +19,9 @@ import { MultiCategoriesQueryDto } from './dto/category.dto'; import { isValidObjectId } from 'mongoose'; import { ExceptionMessage } from '~/shared/constants/echo.constant'; import { CategoryModel, CategoryType } from './model/category.model'; +import { NotImplementedRpcExcption } from '~/shared/exceptions/not-implemented-rpc-exception'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; +import { BadRequestRpcExcption } from '~/shared/exceptions/bad-request-rpc-exception'; @Controller() export class PageServiceController { @@ -74,10 +77,7 @@ export class PageServiceController { // 判断必要Query参数是否存在 if (!query) { // 如果没有query 禁止通行 - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.QueryArgIsRequire, - }); + throw new BadRequestRpcExcption(ExceptionMessage.QueryArgIsRequire); } if (tag === true) { return { @@ -100,10 +100,7 @@ export class PageServiceController { .lean(); if (!data) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.CantFindCategory, - }); + throw new NotFoundRpcExcption(ExceptionMessage.CantFindCategory); } const children = @@ -270,9 +267,6 @@ export class PageServiceController { @MessagePattern({ cmd: PostEvents.PostThumbUp }) @ApiOperation({ summary: '点赞文章' }) async thumbUpPost(_id: string) { - throw new RpcException({ - code: HttpStatus.NOT_IMPLEMENTED, - message: 'Not Implemented', - }); + throw new NotImplementedRpcExcption('Not Implemented'); } } diff --git a/apps/page-service/src/page-service.service.ts b/apps/page-service/src/page-service.service.ts index 636786ad0..1eaf622a8 100644 --- a/apps/page-service/src/page-service.service.ts +++ b/apps/page-service/src/page-service.service.ts @@ -7,8 +7,8 @@ * Coding With IU */ -import { HttpStatus, Inject, Injectable } from '@nestjs/common'; -import { ClientProxy, RpcException } from '@nestjs/microservices'; +import { Inject, Injectable } from '@nestjs/common'; +import { ClientProxy } from '@nestjs/microservices'; import { ModelType } from '@typegoose/typegoose/lib/types'; import { isDefined } from 'class-validator'; import { omit } from 'lodash'; @@ -18,6 +18,7 @@ import { ExceptionMessage } from '~/shared/constants/echo.constant'; import { NotificationEvents } from '~/shared/constants/event.constant'; import { ServicesEnum } from '~/shared/constants/services.constant'; import { PagerDto } from '~/shared/dto/pager.dto'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; import { InjectModel } from '~/shared/transformers/model.transformer'; import { PageModel } from './model/page.model'; @@ -30,7 +31,7 @@ export class PageService { @Inject(ServicesEnum.notification) private readonly notification: ClientProxy, - ) {} + ) { } public get model() { return this.pageModel; @@ -53,10 +54,7 @@ export class PageService { async getPageById(id: string) { const res = this.model.findById(id).lean({ getters: true }); if (!res) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PageIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PageIsNotExist); } return res; } @@ -67,10 +65,7 @@ export class PageService { .lean({ getters: true }) .then((page) => { if (!page) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PageIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PageIsNotExist); } if (!isMaster && page.password) { if (!password || password !== page.password) { @@ -112,10 +107,7 @@ export class PageService { ) .lean({ getters: true }); if (!res) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PageIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PageIsNotExist); } this.notification.emit(NotificationEvents.SystemPageUpdate, res); return res; diff --git a/apps/page-service/src/post-service.service.ts b/apps/page-service/src/post-service.service.ts index 5e02586fc..5878803d6 100644 --- a/apps/page-service/src/post-service.service.ts +++ b/apps/page-service/src/post-service.service.ts @@ -1,5 +1,5 @@ import slugify from 'slugify'; -import { forwardRef, HttpStatus, Inject, Injectable } from '@nestjs/common'; +import { forwardRef, Inject, Injectable } from '@nestjs/common'; import { AggregatePaginateModel, Document, @@ -13,11 +13,13 @@ import { CategoryService } from './category.service'; import { PostModel } from './model/post.model'; import { isDefined } from 'class-validator'; import { omit } from 'lodash'; -import { ClientProxy, RpcException } from '@nestjs/microservices'; +import { ClientProxy } from '@nestjs/microservices'; import { ExceptionMessage } from '~/shared/constants/echo.constant'; import { ModelType } from '@typegoose/typegoose/lib/types'; import { ServicesEnum } from '~/shared/constants/services.constant'; import { NotificationEvents } from '~/shared/constants/event.constant'; +import { BadRequestRpcExcption } from '~/shared/exceptions/bad-request-rpc-exception'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; @Injectable() export class PostService { @@ -31,7 +33,7 @@ export class PostService { @Inject(ServicesEnum.notification) private readonly notification: ClientProxy, - ) {} + ) { } get model() { return this.postModel; @@ -103,13 +105,13 @@ export class PostService { { $sort: sortBy ? { - [sortBy]: sortOrder as any, - } + [sortBy]: sortOrder as any, + } : { - sortField: -1, // sort by our computed field - pin: -1, - created: -1, // and then by the "created" field - }, + sortField: -1, // sort by our computed field + pin: -1, + created: -1, // and then by the "created" field + }, }, { $project: { @@ -161,10 +163,7 @@ export class PostService { async getPostBySlug(slug: string) { const model = await this.model.findOne({ slug }); if (!model) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PostIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PostIsNotExist); } return model; } @@ -172,10 +171,7 @@ export class PostService { async getPostByID(id: string) { const model = await this.model.findById(id); if (!model) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PostIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PostIsNotExist); } return model; } @@ -193,10 +189,7 @@ export class PostService { category, ); if (!categoryDocument) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.CategoryIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.CategoryIsNotExist); } const postDocument = await ( await this.model @@ -206,10 +199,7 @@ export class PostService { }) .then((post) => { if (!post || (!isMaster && post.hide)) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.PostIsNotExist, - }); + throw new NotFoundRpcExcption(ExceptionMessage.PostIsNotExist); } if (!isMaster && post.password) { if (!password || password !== post.password) { @@ -248,19 +238,13 @@ export class PostService { categoryId as any as string, ); if (!category) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.CategoryIsNotExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.CategoryIsNotExist); } const slug = post.slug ? slugify(post.slug) : slugify(post.title); const isAvailableSlug = await this.isAvailablePostSlug(slug); if (!isAvailableSlug) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.SlugIsExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.SlugIsExist); } const res = await this.postModel.create({ ...post, @@ -283,10 +267,7 @@ export class PostService { async updatePostById(id: string, data: Partial) { const oldDocument = await this.postModel.findById(id).lean(); if (!oldDocument) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.PostIsNotExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.PostIsNotExist); } const { categoryId } = data; if (categoryId && categoryId !== oldDocument.categoryId) { @@ -294,10 +275,7 @@ export class PostService { categoryId as any as string, ); if (!category) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.CategoryIsNotExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.CategoryIsNotExist); } } if ([data.text, data.title, data.slug].some((i) => isDefined(i))) { @@ -307,20 +285,14 @@ export class PostService { const originDocument = await this.postModel.findById(id); if (!originDocument) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.PostIsNotExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.PostIsNotExist); } if (data.slug && data.slug !== originDocument.slug) { data.slug = slugify(data.slug); // 检查slug是否已存在 const isAvailableSlug = await this.isAvailablePostSlug(data.slug); if (!isAvailableSlug) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.SlugIsExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.SlugIsExist); } } Object.assign(originDocument, omit(data, PostModel.protectedKeys)); diff --git a/apps/user-service/src/user-service.service.ts b/apps/user-service/src/user-service.service.ts index ddc293449..2cb2cd8ff 100644 --- a/apps/user-service/src/user-service.service.ts +++ b/apps/user-service/src/user-service.service.ts @@ -1,5 +1,5 @@ -import { HttpStatus, Inject, Injectable, Logger } from '@nestjs/common'; -import { ClientProxy, RpcException } from '@nestjs/microservices'; +import { Inject, Injectable, Logger } from '@nestjs/common'; +import { ClientProxy } from '@nestjs/microservices'; import { ReturnModelType } from '@typegoose/typegoose'; import { compareSync } from 'bcrypt'; // import { nanoid } from 'nanoid'; @@ -8,6 +8,9 @@ import { IpRecord } from '~/shared/common/decorator/ip.decorator'; import { ExceptionMessage } from '~/shared/constants/echo.constant'; import { NotificationEvents } from '~/shared/constants/event.constant'; import { ServicesEnum } from '~/shared/constants/services.constant'; +import { BadRequestRpcExcption } from '~/shared/exceptions/bad-request-rpc-exception'; +import { ForbiddenRpcExcption } from '~/shared/exceptions/forbidden-rpc-exception'; +import { NotFoundRpcExcption } from '~/shared/exceptions/not-found-rpc-exception'; import { InjectModel } from '~/shared/transformers/model.transformer'; import { getAvatar } from '~/shared/utils'; import { LoginDto } from './user.dto'; @@ -25,7 +28,7 @@ export class UserService { @Inject(ServicesEnum.notification) private readonly notification: ClientProxy, - ) {} + ) { } public get model() { return this.userModel; } @@ -43,10 +46,7 @@ export class UserService { const exist = await this.userModel.findOne({ username: model.username }); if (exist) - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.UserNameIsExist, - }); + throw new ForbiddenRpcExcption(ExceptionMessage.UserNameIsExist); const userCount = await this.userModel.countDocuments(); @@ -54,10 +54,7 @@ export class UserService { const hasMaster = !!userCount; // 禁止注册两个以上账户 if (hasMaster) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.UserIsExist, - }); + throw new BadRequestRpcExcption(ExceptionMessage.UserIsExist); } const res = await this.userModel.create({ @@ -79,16 +76,10 @@ export class UserService { async returnLoginData(username: string, password: string) { const user = await this.userModel.findOne({ username }).select('+password'); if (!user) { - throw new RpcException({ - code: HttpStatus.FORBIDDEN, - message: ExceptionMessage.UserNameError, - }); + throw new ForbiddenRpcExcption(ExceptionMessage.UserNameError); } if (!compareSync(password, user.password)) { - throw new RpcException({ - code: HttpStatus.FORBIDDEN, - message: ExceptionMessage.UserPasswordError, - }); + throw new ForbiddenRpcExcption(ExceptionMessage.UserPasswordError); } return user; @@ -148,10 +139,7 @@ export class UserService { .select(`${getLoginIp ? ' +lastLoginIp' : ''}`) .lean({ virtuals: true }); if (!user) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.UserNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.UserNotFound); } return user; } @@ -171,18 +159,12 @@ export class UserService { .select('+password +apiToken'); if (!currentUser) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.UserNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.UserNotFound); } // 1. 验证新旧密码是否一致 const isSamePassword = compareSync(password, currentUser.password); if (isSamePassword) { - throw new RpcException({ - code: HttpStatus.BAD_REQUEST, - message: ExceptionMessage.UserPasswordIsSame, - }); + throw new BadRequestRpcExcption(ExceptionMessage.UserPasswordIsSame); } // // 2. 认证码重新生成 @@ -210,10 +192,7 @@ export class UserService { ): Promise> { const master = await this.userModel.findOne({ username }); if (!master) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.UserNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.UserNotFound); } const PrevFootstep = { lastLoginTime: master.lastLoginTime || new Date(1586090559569), @@ -231,10 +210,7 @@ export class UserService { async getMaster() { const master = await this.userModel.findOne({ role: UserRole.root }); if (!master) { - throw new RpcException({ - code: HttpStatus.NOT_FOUND, - message: ExceptionMessage.UserNotFound, - }); + throw new NotFoundRpcExcption(ExceptionMessage.UserNotFound); } return master; } diff --git a/shared/exceptions/bad-request-rpc-exception.ts b/shared/exceptions/bad-request-rpc-exception.ts new file mode 100644 index 000000000..c42e93abf --- /dev/null +++ b/shared/exceptions/bad-request-rpc-exception.ts @@ -0,0 +1,8 @@ +import { HttpStatus } from "@nestjs/common"; +import { RpcException } from "~/shared/exceptions/rpc-exception"; + +export class BadRequestRpcExcption extends RpcException { + constructor(readonly message: string) { + super(message, HttpStatus.BAD_REQUEST); + } +} \ No newline at end of file diff --git a/shared/exceptions/forbidden-rpc-exception.ts b/shared/exceptions/forbidden-rpc-exception.ts new file mode 100644 index 000000000..cbd79bd53 --- /dev/null +++ b/shared/exceptions/forbidden-rpc-exception.ts @@ -0,0 +1,8 @@ +import { HttpStatus } from "@nestjs/common"; +import { RpcException } from "~/shared/exceptions/rpc-exception"; + +export class ForbiddenRpcExcption extends RpcException { + constructor(readonly message: string) { + super(message, HttpStatus.FORBIDDEN); + } +} \ No newline at end of file diff --git a/shared/exceptions/not-found-rpc-exception.ts b/shared/exceptions/not-found-rpc-exception.ts new file mode 100644 index 000000000..f75743c26 --- /dev/null +++ b/shared/exceptions/not-found-rpc-exception.ts @@ -0,0 +1,8 @@ +import { HttpStatus } from "@nestjs/common"; +import { RpcException } from "~/shared/exceptions/rpc-exception"; + +export class NotFoundRpcExcption extends RpcException { + constructor(readonly message: string) { + super(message, HttpStatus.NOT_FOUND); + } +} \ No newline at end of file diff --git a/shared/exceptions/not-implemented-rpc-exception.ts b/shared/exceptions/not-implemented-rpc-exception.ts new file mode 100644 index 000000000..e10f33b02 --- /dev/null +++ b/shared/exceptions/not-implemented-rpc-exception.ts @@ -0,0 +1,8 @@ +import { HttpStatus } from "@nestjs/common"; +import { RpcException } from "~/shared/exceptions/rpc-exception"; + +export class NotImplementedRpcExcption extends RpcException { + constructor(readonly message: string) { + super(message, HttpStatus.NOT_IMPLEMENTED); + } +} \ No newline at end of file diff --git a/shared/exceptions/rpc-exception.ts b/shared/exceptions/rpc-exception.ts new file mode 100644 index 000000000..2a519f145 --- /dev/null +++ b/shared/exceptions/rpc-exception.ts @@ -0,0 +1,19 @@ +import { RpcException as _RpcException } from "@nestjs/microservices"; + + +/** + * @examples + * throw new RpcException('Forbidden', HttpStatus.FORBIDDEN); + */ +export class RpcException extends _RpcException { + constructor(private readonly mes: string, private readonly status: number) { + super(mes); + } + + public getError(): object { + return { + code: this.status, + message: this.mes, + }; + } +} \ No newline at end of file diff --git a/shared/exceptions/unauthorized-rpc-exception.ts b/shared/exceptions/unauthorized-rpc-exception.ts new file mode 100644 index 000000000..52a435fe9 --- /dev/null +++ b/shared/exceptions/unauthorized-rpc-exception.ts @@ -0,0 +1,8 @@ +import { HttpStatus } from "@nestjs/common"; +import { RpcException } from "~/shared/exceptions/rpc-exception"; + +export class UnauthorizedRpcExcption extends RpcException { + constructor(readonly message: string) { + super(message, HttpStatus.UNAUTHORIZED); + } +} \ No newline at end of file