The error I am encountering – TypeError: response.status is not a function – suggests that the response object from Fastify is not behaving as expected. This can happen due to a few reasons, including incorrect usage of the Fastify reply object, or possible conflicts in the response lifecycle.
I have also took into consideration this Warning
If you are using @nestjs/platform-fastify you can use response.send() instead of response.json(). Don't forget to import the correct types from fastify.`
Here is the code
`@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
constructor(private logger: LoggerService) {}
async catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const res = ctx.getResponse<FastifyReply>();
const req = ctx.getRequest<FastifyRequest>();
const status = exception.getStatus?.() || 500;
const errorResponse = {
status,
message: exception.message || 'Unexpected error',
path: req.url,
method: req.method,
timestamp: new Date().toISOString(),
};
const [sec, nano] = process.hrtime();
const responseTime = sec * 1e3 + nano / 1e6;
this.logError(status, responseTime, req, exception, errorResponse);
res.status(status).send(errorResponse);
}
private logError(status: number, time: number, req: FastifyRequest, ex: HttpException, errRes: any) {
const logData = { status, time: time.toFixed(3), path: req.url, msg: ex.message };
this.logger.error(status === 404 ? 'API Error' : 'Handled Exception', logData);
}
}`
I have tried to use the httpAdapter as well:
httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus);