0

Does anybody can suggest solution, how to render page in custom HttpExceptionFilter in nest.js, when using Fastify and ejs. Below current HttpExceptionFilter:

import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';
import { FastifyRequest, FastifyReply } from 'fastify';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response: FastifyReply<any> = ctx.getResponse();
    const request: FastifyRequest = ctx.getRequest();

    const status =
      exception instanceof HttpException
        ? exception.getStatus()
        : HttpStatus.INTERNAL_SERVER_ERROR;

    const objResponse = Object.assign(exception, {
      timestamp: new Date().toISOString(),
      path: request.req.url
    });

    if (status === HttpStatus.UNAUTHORIZED)
      return // page render with some content;

    if (status === HttpStatus.NOT_FOUND)
      return // page render with some content;

    if (status === HttpStatus.INTERNAL_SERVER_ERROR) {
      if (process.env.NODE_ENV === 'production') {
        return // page render with some content;
      }
      else {
        return response.status(status).send(objResponse);
      }
    }
  }
}

user2443795
  • 144
  • 7
  • did you tried `response.view('/templates/index.ejs', { text: 'text' })` ? – Manuel Spigolon Apr 22 '20 at 16:07
  • response.view - it's for Express, doesn't work with Fastify :((( – user2443795 Apr 22 '20 at 16:43
  • 1
    Not true, fastify add a [`reply` decorator](https://github.com/fastify/point-of-view#usage) called `view` to render a page when you need it. I don't know nestjs, so I don't know if it is removing this kind of feature from fastify – Manuel Spigolon Apr 23 '20 at 07:33

0 Answers0