0

I am working on a NestJS application where I have a controller method that fetches new users from DB. During this process, an error should be thrown under certain conditions. But my frontend desktop app receives a 200 status without the error message. Interestingly, the error is correctly propagated when tested via Swagger or Postman.
(@nestjs/swagger": "^6.1.3")

Is there a problem with the code or I just don't understand how the errors are processed? Here is the code from the controller to the repository:

My Controller:

@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);
}

My service:

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);
    }
}

My repository:

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;
}

Problem: When the condition to throw new Error('REG_QUE_LOCK_INACTIVITY') is met, my desktop app receives a 200 status code without any error message, while the error is correctly shown in Swagger or Postman. Why is this discrepancy happening, and how can I ensure that the error is correctly propagated to the frontend?

Any insights on why this might be happening and how to fix it would be greatly appreciated.

1 Answer 1

0

So, I don't know why the error is not thrown, but this is the solution. I added timeout and that works. Maybe it can be done differently too, but this works.

setTimeout(() => {
  this.unlockNewRegistrations(adminId, product);
}, 100);
throw new Error(REG_QUE_LOCK_INACTIVITY);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.