如何在nestjs中return自定义状态码?
How to return custom status code in nestjs?
我的系统有这样的自定义错误:
import { ExtendableError } from './extandable.error'
export const customError = {
EMAIL_EXIST: () => new ExtendableError({
message: 'USER WITH SUCH EMAIL ALREADY EXIST',
code: 403
}),
INVALID_EMAIL: () => new ExtendableError({
message: 'INVALID EMAIL',
code: 400
}),
INVALID_PASSWORD: () => new ExtendableError({
message: 'INVALID EMAIL OR PASSWORD',
code: 403
}),
EMAIL_DOES_NOT_EXIST: () => new ExtendableError({
message: 'EMAIL DOES NOT EXIST',
code: 404
}),
TOKEN_DOES_NOT_EXIST: () => new ExtendableError({
message: 'TOKEN DOES NOT EXIST',
code: 404
}),
DIFFERENT_PASSWORDS: () => new ExtendableError({
message: 'PASSWORDS ARE NOT SAME',
code: 400
}),
CURRENT_PASSWORD_ERR0R: () => new ExtendableError({
message: 'CURRENT PASSWORD IS INCORRECT',
code: 400
}),
SAME_PASSWORDS_ERROR: () => new ExtendableError({
message: 'CANNOT CHANGE SET NEW PASSWORD AS OLD PASSWORD',
code: 403
})
}
在这里我可以写我自己的错误响应
当我请求某些东西时,例如电子邮件存在,它会抛出一个错误,即注册时此类电子邮件已经存在。
这里写着:
async createAccount (doc: RegisterDto, verificationLink: string) {
if (!isValidEmail(doc.email)) {
return customError.INVALID_EMAIL()
}
const user = await this.existByEmail(doc.email)
if (!user) {
return customError.EMAIL_EXIST()
}// HERE I RETURNING AN ERROR TO CONTROLLER
doc.password = await bcrypt.hash(doc.password, SALT_ROUNDS)
const created = await this.repository.create(this.registerMapper.toDomain(doc))
await this.emailService.sendVerificationEmail(created, verificationLink)
return created
}
这是我在控制器中的功能:
@Post('/api/register')
async register(@Body() registerDTO: RegisterDto, @Request() request, @Response() response) {
const verificationLink = `${request.protocol}://${request.header.host}/api/verify-account/`
return await this.service.createAccount(registerDTO, verificationLink)
}
在邮递员中,它显示了正文和响应,但状态代码的实际结果有 201
代码
我该如何解决?
而不是 return customError.INVALID_EMAIL()
你应该 throw
一个错误。
来自 npm docs:
let ExtendableError = require('extendable-error');
class MyCustomError extends ExtendableError {};
throw new MyCustomError('A custom error ocurred!');
我的系统有这样的自定义错误:
import { ExtendableError } from './extandable.error'
export const customError = {
EMAIL_EXIST: () => new ExtendableError({
message: 'USER WITH SUCH EMAIL ALREADY EXIST',
code: 403
}),
INVALID_EMAIL: () => new ExtendableError({
message: 'INVALID EMAIL',
code: 400
}),
INVALID_PASSWORD: () => new ExtendableError({
message: 'INVALID EMAIL OR PASSWORD',
code: 403
}),
EMAIL_DOES_NOT_EXIST: () => new ExtendableError({
message: 'EMAIL DOES NOT EXIST',
code: 404
}),
TOKEN_DOES_NOT_EXIST: () => new ExtendableError({
message: 'TOKEN DOES NOT EXIST',
code: 404
}),
DIFFERENT_PASSWORDS: () => new ExtendableError({
message: 'PASSWORDS ARE NOT SAME',
code: 400
}),
CURRENT_PASSWORD_ERR0R: () => new ExtendableError({
message: 'CURRENT PASSWORD IS INCORRECT',
code: 400
}),
SAME_PASSWORDS_ERROR: () => new ExtendableError({
message: 'CANNOT CHANGE SET NEW PASSWORD AS OLD PASSWORD',
code: 403
})
}
在这里我可以写我自己的错误响应 当我请求某些东西时,例如电子邮件存在,它会抛出一个错误,即注册时此类电子邮件已经存在。
这里写着:
async createAccount (doc: RegisterDto, verificationLink: string) {
if (!isValidEmail(doc.email)) {
return customError.INVALID_EMAIL()
}
const user = await this.existByEmail(doc.email)
if (!user) {
return customError.EMAIL_EXIST()
}// HERE I RETURNING AN ERROR TO CONTROLLER
doc.password = await bcrypt.hash(doc.password, SALT_ROUNDS)
const created = await this.repository.create(this.registerMapper.toDomain(doc))
await this.emailService.sendVerificationEmail(created, verificationLink)
return created
}
这是我在控制器中的功能:
@Post('/api/register')
async register(@Body() registerDTO: RegisterDto, @Request() request, @Response() response) {
const verificationLink = `${request.protocol}://${request.header.host}/api/verify-account/`
return await this.service.createAccount(registerDTO, verificationLink)
}
在邮递员中,它显示了正文和响应,但状态代码的实际结果有 201
代码
我该如何解决?
而不是 return customError.INVALID_EMAIL()
你应该 throw
一个错误。
来自 npm docs:
let ExtendableError = require('extendable-error');
class MyCustomError extends ExtendableError {};
throw new MyCustomError('A custom error ocurred!');