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.
(@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);
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. 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.