NarHakobyan / awesome-nest-boilerplate

Awesome NestJS Boilerplate 😍, Typescript 💪, Postgres 🎉, TypeORM 🥳
https://narhakobyan.github.io/awesome-nest-boilerplate
MIT License
2.34k stars 441 forks source link

TypeError: Cannot read properties of undefined (reading 'split') #273

Closed vanenshi closed 2 years ago

vanenshi commented 2 years ago

I clone the project right now and this error pops up every time there is an exception (HTTP handled exception).

TypeError: Cannot read properties of undefined (reading 'split')
    at I18nService.translateObject (D:\awesome-nest-boilerplate\node_modules\nestjs-i18n\src\services\i18n.service.ts:128:22)
    at I18nService.translate (D:\awesome-nest-boilerplate\node_modules\nestjs-i18n\src\services\i18n.service.ts:62:36)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async AllExceptionsFilter.catch (D:\awesome-nest-boilerplate\src\filters\all-exceptions.filter.ts:22:15)

and here is the log of the exception that is causing this error:

// all-exceptions.filter.ts
// console.log(JSON.stringify(exception, null, 2));
{
  "response": {
    "statusCode": 404,
    "message": "Cannot GET /",
    "error": "Not Found"
  },
  "status": 404,
  "message": "Cannot GET /",
  "name": "NotFoundException"
}

and

// all-exceptions.filter.ts
// console.log(JSON.stringify(exception.getResponse(), null, 2));
{
  "statusCode": 404,
  "message": "Cannot GET /",
  "error": "Not Found"
}
mouheb1 commented 2 years ago

same

did you fix it ?

NarHakobyan commented 2 years ago

@mouheb1 when does it happen?

vanenshi commented 2 years ago

@mouheb1 when does it happen?

I found the source of the problem. in the all-exceptions.filter.ts the translate function causes the error. I manage to temporarily fix the problem by adding a try ... cache to the message translator. here is my code

// filters/all-exceptions.filter.ts

import type { ArgumentsHost, ExceptionFilter } from "@nestjs/common";
import { Catch, HttpException } from "@nestjs/common";
import type { Response } from "express";
import { I18nService } from "nestjs-i18n";

@Catch(HttpException)
export class AllExceptionsFilter implements ExceptionFilter {
  constructor(private readonly i18n: I18nService) {}

  async catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const statusCode = exception.getStatus();

    let message = exception.getResponse() as {
      key: string;
      args: Record<string, unknown>;
    };

    // add try cache because of a error in translate function
    try {
      message = await this.i18n.translate(message.key, {
        lang: ctx.getRequest().i18nLang,
        args: message.args,
      });
    } catch (e) {}

    response.status(statusCode).json({ statusCode, message });
  }
}
mouheb1 commented 2 years ago

@NarHakobyan it happened when i tried to use an API without being authenticated so i fixed this issue by removing the nexjs-i18n

@vanenshi but you are catching the error and not fixing it so it's like turning around the issue

vanenshi commented 2 years ago

@NarHakobyan it happened when i tried to use an API without being authenticated so i fixed this issue by removing the nexjs-i18n

@vanenshi but you are catching the error and not fixing it so it's like turning around the issue

as I told you in the previous comment, I just temporarily fix the problem to be able to use the API