toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
656 stars 113 forks source link

GraphQL Internal Server Error w/ I18N #669

Open G0maa opened 1 month ago

G0maa commented 1 month ago

Describe the bug

Hello, thanks for the package! :rose:

As in the provided code sample, RESTful requests work correctly (when validation fails or passes), GraphQL requests fail when validation fails, resulting in this error:

{
  "errors": [
    {
      "message": "Bad Request",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "helloWorldI18n"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "I18nValidationException: Bad Request",
          "    at I18nValidationPipe.i18nValidationErrorFactory (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/nestjs-i18n/src/utils/util.ts:34:10)",
          "    at I18nValidationPipe.transform (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/common/pipes/validation.pipe.js:74:30)",
          "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
          "    at async resolveParamValue (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:136:31)",
          "    at async Promise.all (index 0)",
          "    at async pipesFn (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:138:13)",
          "    at async /home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:66:17"
        ]
      }
    }
  ],
  "data": null
}

Any help is appreciated!

Reproduction

https://github.com/G0maa/i18n-graphql

System Info

System:
    OS: Linux 6.8 Zorin OS 17.2 17.2
    CPU: (12) x64 AMD Ryzen 5 3600 6-Core Processor
    Memory: 5.09 GB / 15.53 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 20.14.0 - ~/.asdf/installs/nodejs/20.14.0/bin/node
    npm: 10.7.0 - ~/.asdf/plugins/nodejs/shims/npm
    pnpm: 9.1.0 - ~/.asdf/installs/nodejs/20.14.0/bin/pnpm
  Browsers:
    Chrome: 129.0.6668.89

Used Package Manager

npm

Validations

G0maa commented 1 month ago

I just noticed I forgot to add: app.useGlobalPipes(new ValidationPipe());

In main.ts.

EDIT: GraphQL helloWorld query, i.e. class-validator without i18n now works correctly I think:

{
  "errors": [
    {
      "message": "Bad Request Exception",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "helloWorld"
      ],
      "extensions": {
        "code": "BAD_REQUEST",
        "stacktrace": [
          "BadRequestException: Bad Request Exception",
          "    at ValidationPipe.exceptionFactory (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/common/pipes/validation.pipe.js:101:20)",
          "    at ValidationPipe.transform (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/common/pipes/validation.pipe.js:74:30)",
          "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
          "    at async /home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/pipes/pipes-consumer.js:15:25",
          "    at async resolveParamValue (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:136:31)",
          "    at async Promise.all (index 0)",
          "    at async pipesFn (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:138:13)",
          "    at async /home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:66:17"
        ],
        "originalError": {
          "message": [
            "page must not be less than 1"
          ],
          "error": "Bad Request",
          "statusCode": 400
        }
      }
    }
  ],
  "data": null
}

And this is the result of helloWorldI18n:

{
  "errors": [
    {
      "message": "Bad Request Exception",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "helloWorldI18n"
      ],
      "extensions": {
        "code": "BAD_REQUEST",
        "stacktrace": [
          "BadRequestException: Bad Request Exception",
          "    at ValidationPipe.exceptionFactory (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/common/pipes/validation.pipe.js:101:20)",
          "    at ValidationPipe.transform (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/common/pipes/validation.pipe.js:74:30)",
          "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)",
          "    at async /home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/pipes/pipes-consumer.js:15:25",
          "    at async resolveParamValue (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:136:31)",
          "    at async Promise.all (index 0)",
          "    at async pipesFn (/home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:138:13)",
          "    at async /home/gomaa-zorin/Github/GE-Techs/i18n-graphql/node_modules/@nestjs/core/helpers/external-context-creator.js:66:17"
        ],
        "originalError": {
          "message": [
            "validation.Generic|{\"value\":-1,\"constraints\":[1]}"
          ],
          "error": "Bad Request",
          "statusCode": 400
        }
      }
    }
  ],
  "data": null
}

Hence the "validation.Generic|{\"value\":-1,\"constraints\":[1]}".

G0maa commented 1 month ago

I got this error in some other project (similar configuration, not identical), dunno if it's related or not:

{
  "errors": [
    {
      "message": "Cannot read properties of undefined (reading 'service')",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "shoppingLists"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "TypeError: Cannot read properties of undefined (reading 'service')",
          "    at I18nValidationExceptionFilter.catch (/home/gomaa-zorin/Github/GE-Techs/rasheed-backend/node_modules/nestjs-i18n/src/filters/i18n-validation-exception.filter.ts:31:66)",
          "    at ExternalExceptionsHandler.invokeCustomFilters (/home/gomaa-zorin/Github/GE-Techs/rasheed-backend/node_modules/@nestjs/core/exceptions/external-exceptions-handler.js:31:32)",
          "    at ExternalExceptionsHandler.next (/home/gomaa-zorin/Github/GE-Techs/rasheed-backend/node_modules/@nestjs/core/exceptions/external-exceptions-handler.js:14:29)",
          "    at Object.shoppingLists (/home/gomaa-zorin/Github/GE-Techs/rasheed-backend/node_modules/@nestjs/core/helpers/external-proxy.js:14:42)",
          "    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"
        ]
      }
    }
  ],
  "data": null
}