fix: bypass if not init system
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -1,32 +1,34 @@
|
||||
import type { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
|
||||
import { HttpException } from '@nestjs/common'
|
||||
|
||||
import { ErrorCode } from '~/constants/error-code.constant'
|
||||
import { ErrorCode, ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
|
||||
export class BusinessException extends HttpException {
|
||||
public bizCode: ErrorCodeEnum
|
||||
constructor(code: ErrorCodeEnum, extraMessage?: string)
|
||||
constructor(message: string)
|
||||
constructor(...args: any[]) {
|
||||
let status = 500
|
||||
const [code, extraMessage] = args as any
|
||||
const bizError = ErrorCode[code] || []
|
||||
const [bizCode, extraMessage] = args as any
|
||||
const bizError = ErrorCode[bizCode] || []
|
||||
const [message] = bizError
|
||||
status = bizError[1] ?? status
|
||||
|
||||
const isOnlyMessage = typeof code == 'string' && args.length === 1
|
||||
const isOnlyMessage = typeof bizCode == 'string' && args.length === 1
|
||||
|
||||
const jointMessage = isOnlyMessage
|
||||
? code // this code is message
|
||||
? bizCode // this code is message
|
||||
: message + (extraMessage ? `: ${extraMessage}` : '')
|
||||
|
||||
super(
|
||||
HttpException.createBody(
|
||||
{ code, message: jointMessage },
|
||||
{ code: bizCode, message: jointMessage },
|
||||
jointMessage,
|
||||
status,
|
||||
),
|
||||
status,
|
||||
)
|
||||
|
||||
this.bizCode = typeof bizCode === 'number' ? bizCode : ErrorCodeEnum.Default
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export enum ErrorCodeEnum {
|
||||
// app
|
||||
Default = 1,
|
||||
NoContentCanBeModified = 1000,
|
||||
|
||||
// biz
|
||||
@@ -17,6 +18,7 @@ export enum ErrorCodeEnum {
|
||||
|
||||
export const ErrorCode = Object.freeze<Record<ErrorCodeEnum, [string, number]>>(
|
||||
{
|
||||
[ErrorCodeEnum.Default]: ['未知错误', 500],
|
||||
[ErrorCodeEnum.SlugNotAvailable]: ['slug 不可用', 400],
|
||||
[ErrorCodeEnum.MaxCountLimit]: ['已达到最大数量限制', 400],
|
||||
[ErrorCodeEnum.BanInDemo]: ['Demo 模式下此操作不可用', 400],
|
||||
|
||||
@@ -21,10 +21,8 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common'
|
||||
|
||||
import { BusinessException } from '~/common/exceptions/biz.exception'
|
||||
import { CannotFindException } from '~/common/exceptions/cant-find.exception'
|
||||
import { NoContentCanBeModifiedException } from '~/common/exceptions/no-content-canbe-modified.exception'
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
import { DatabaseService } from '~/processors/database/database.service'
|
||||
import { EmailService } from '~/processors/helper/helper.email.service'
|
||||
import { InjectModel } from '~/transformers/model.transformer'
|
||||
@@ -62,7 +60,7 @@ export class CommentService implements OnModuleInit {
|
||||
) {}
|
||||
|
||||
private async getMailOwnerProps() {
|
||||
const masterInfo = await this.userService.getMasterInfo()
|
||||
const masterInfo = await this.userService.getSiteMasterOrMocked()
|
||||
return UserModel.serialize(masterInfo)
|
||||
}
|
||||
async onModuleInit() {
|
||||
@@ -313,9 +311,7 @@ export class CommentService implements OnModuleInit {
|
||||
}
|
||||
|
||||
const masterInfo = await this.userService.getMasterInfo()
|
||||
if (!masterInfo) {
|
||||
throw new BusinessException(ErrorCodeEnum.MasterLost)
|
||||
}
|
||||
|
||||
const refType = model.refType
|
||||
const refModel = this.getModelByRefType(refType)
|
||||
const refDoc = await refModel.findById(model.ref).lean()
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
|
||||
import { UserService } from '../user/user.service'
|
||||
import { SubscribeController } from './subscribe.controller'
|
||||
import { SubscribeService } from './subscribe.service'
|
||||
|
||||
@Module({
|
||||
controllers: [SubscribeController],
|
||||
providers: [SubscribeService],
|
||||
providers: [SubscribeService, UserService],
|
||||
exports: [SubscribeService],
|
||||
})
|
||||
export class SubscribeModule {}
|
||||
|
||||
@@ -20,7 +20,7 @@ import { InjectModel } from '~/transformers/model.transformer'
|
||||
import { hashString, md5 } from '~/utils'
|
||||
|
||||
import { ConfigsService } from '../configs/configs.service'
|
||||
import { UserModel } from '../user/user.model'
|
||||
import { UserService } from '../user/user.service'
|
||||
import { SubscribleMailType } from './subscribe-mail.enum'
|
||||
import {
|
||||
SubscribeNoteCreateBit,
|
||||
@@ -31,7 +31,7 @@ import { defaultSubscribeForRenderProps } from './subscribe.email.default'
|
||||
import { SubscribeModel } from './subscribe.model'
|
||||
|
||||
declare type Email = string
|
||||
declare type Subscribe = number
|
||||
declare type SubscribeBit = number
|
||||
|
||||
@Injectable()
|
||||
export class SubscribeService implements OnModuleInit {
|
||||
@@ -44,9 +44,10 @@ export class SubscribeService implements OnModuleInit {
|
||||
private readonly configService: ConfigsService,
|
||||
private readonly urlBuilderService: UrlBuilderService,
|
||||
private readonly emailService: EmailService,
|
||||
private readonly userService: UserService,
|
||||
) {}
|
||||
|
||||
private subscribeMap = new Map<Email, Subscribe>()
|
||||
private subscribeMap = new Map<Email, SubscribeBit>()
|
||||
get model() {
|
||||
return this.subscribeModel
|
||||
}
|
||||
@@ -56,7 +57,7 @@ export class SubscribeService implements OnModuleInit {
|
||||
}
|
||||
|
||||
private async registerEmailTemplate() {
|
||||
const owner = UserModel.serialize(await this.configService.getMaster())
|
||||
const owner = await this.userService.getSiteMasterOrMocked()
|
||||
const renderProps: SubscribeTemplateRenderProps = {
|
||||
...defaultSubscribeForRenderProps,
|
||||
aggregate: {
|
||||
|
||||
@@ -11,7 +11,10 @@ import {
|
||||
} from '@nestjs/common'
|
||||
import { ReturnModelType } from '@typegoose/typegoose'
|
||||
|
||||
import { BusinessException } from '~/common/exceptions/biz.exception'
|
||||
import {
|
||||
BizException,
|
||||
BusinessException,
|
||||
} from '~/common/exceptions/biz.exception'
|
||||
import { ErrorCodeEnum } from '~/constants/error-code.constant'
|
||||
import { InjectModel } from '~/transformers/model.transformer'
|
||||
import { getAvatar, sleep } from '~/utils'
|
||||
@@ -50,7 +53,7 @@ export class UserService {
|
||||
.select(`${getLoginIp ? ' +lastLoginIp' : ''}`)
|
||||
.lean({ virtuals: true })
|
||||
if (!user) {
|
||||
throw new BadRequestException('没有完成初始化!')
|
||||
throw new BizException(ErrorCodeEnum.MasterLost)
|
||||
}
|
||||
const avatar = user.avatar ?? getAvatar(user.mail)
|
||||
return { ...user, avatar }
|
||||
@@ -142,4 +145,23 @@ export class UserService {
|
||||
this.Logger.warn(`主人已登录,IP: ${ip}`)
|
||||
return PrevFootstep as any
|
||||
}
|
||||
|
||||
async getSiteMasterOrMocked() {
|
||||
return await this.getMasterInfo().catch((err) => {
|
||||
if (
|
||||
err instanceof BusinessException &&
|
||||
err.bizCode === ErrorCodeEnum.MasterLost
|
||||
) {
|
||||
return {
|
||||
id: '1',
|
||||
name: '站长大人',
|
||||
mail: 'example@owner.com',
|
||||
|
||||
username: 'johndoe',
|
||||
created: new Date('2021/1/1 10:00:11'),
|
||||
} as UserModel
|
||||
}
|
||||
throw err
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user