NestJS:抛出的错误未传播到前端,而是返回 200 状态

问题描述 投票:0回答:1

我正在开发一个 NestJS 应用程序,其中我有一个从数据库获取新用户的控制器方法。在此过程中,在某些条件下应该抛出错误。但我的前端桌面应用程序收到 200 状态,但没有错误消息。有趣的是,当通过 Swagger 或 Postman 测试时,错误会正确传播。
(

@nestjs/swagger": "^6.1.3"
)

代码有问题还是我只是不明白错误是如何处理的?这是从控制器到存储库的代码:

我的控制器:

@ApiQuery({
    name: 'offset',
    type: 'number',
    required: false,
})
@ApiQuery({
    name: 'limit',
    type: 'number',
    required: false,
})
@ApiQuery({
    name: 'sort',
    type: 'string',
    required: false,
})
@ApiResponse({
    status: HttpStatus.FORBIDDEN,
    description: 'The server refuses to authorize the request.',
})
@ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: 'The request is malformed or invalid.',
})
@ApiResponse({
    status: HttpStatus.NOT_FOUND,
    description: 'The server did not find anything matching the request.',
})
@ApiResponse({
    status: HttpStatus.REQUEST_TIMEOUT,
    description: 'The client did not produce a request within the expected time.',
})
@ApiResponse({
    status: HttpStatus.INTERNAL_SERVER_ERROR,
    description: 'The server encountered an unexpected condition.',
})
@Get('users/new-registrations')
async getNewRegistrations(
    @Query() { offset = 0, limit = 50, sort }:
        { offset: number, limit: number, sort?: string },
): Promise<RegistrationsResponseDto | void> {
        const dto = new RegQueryOptionsDto();
        dto.offset = offset;
        dto.limit = limit;
        dto.sort = sort;
        return await this.registrationService.getNewRegistrations(dto);
}

我的服务:

async getNewRegistrations(
    dto: RegQueryOptionsDto,
): Promise<RegistrationsResponseDto | void> {
    const adminId = 0;
    const product = 0;

    await this.isRegQueueLocked(adminId, product);
    return await this.fetchNewRegistrations(dto, adminId, product);
}

async isRegQueueLocked(adminId: string, product: Product): Promise<void> {
    const lockTime = 30000; //30secs
    const lockedByAdminId = await this.usersRepository.getAdminIdIfRegistrationsLocked(adminId, lockTime, product);
    if (lockedByAdminId !== null && lockedByAdminId !== adminId) {
        await this.throwExceptionWithAdminData(lockedByAdminId);
    }
}

我的存储库:

async getAdminIdIfRegistrationsLocked(
    adminId: string,
    lockTime: number,
    product: Product,
): Promise<string | null> {
    const registrationQueueEntity: RegistrationQueueEntity | null =
        await this.registrationQueueRepository.findOne({
            where: {
                product,
            },
        });
    if (registrationQueueEntity &&
        registrationQueueEntity.lockedTimestamp &&
        registrationQueueEntity.lockedByUser
    ) {
        if (Date.now() < (Number(registrationQueueEntity.lockedTimestamp) || 0) + lockTime) {
            return registrationQueueEntity.lockedByUser;
        } else if (registrationQueueEntity.lockedByUser === adminId) {
            await this.unlockNewRegistrations(adminId, product); //If I remove this line, error is thrown correctly
            throw new Error('REG_QUE_LOCK_INACTIVITY'); //408
        }
    }
    return null;
}

问题: 当满足

throw new Error('REG_QUE_LOCK_INACTIVITY')
的条件时,我的桌面应用程序会收到 200 状态代码,没有任何错误消息,而错误会在 Swagger 或 Postman 中正确显示。为什么会发生这种差异?如何确保错误正确传播到前端?

任何关于为什么会发生这种情况以及如何解决它的见解将不胜感激。

error-handling nestjs http-status-codes
1个回答
0
投票

所以,我不知道为什么没有抛出错误,但这就是解决方案。我添加了超时并且有效。也许也可以用不同的方式来做,但这很有效。

setTimeout(() => {
  this.unlockNewRegistrations(adminId, product);
}, 100);
throw new Error(REG_QUE_LOCK_INACTIVITY);
© www.soinside.com 2019 - 2024. All rights reserved.