Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseExceptionFilter provider for WebSocket using WS doesn't send error messages to client #11433

Closed
3 of 15 tasks
dieeisenefaust opened this issue Apr 6, 2023 · 1 comment
Closed
3 of 15 tasks
Labels
needs triage This issue has not been looked into

Comments

@dieeisenefaust
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

``I have spent pretty extensive time looking for an answer to this situation and the very few instances I found where it was mentioned failed to address the primary issue or provide a clear answer.

When running a WebSocket gateway using the WS platform and using the BaseWsExceptionFilter to catch validation errors thrown by the ValidationPipe provider applied to the gateway, the error is logged to the console on the server side but nothing is sent to the client.

I believe this may be due to the fact that the BaseWsExceptionFilter uses client.emit('exception', message) which, I believe, is Socket.io specific.

Gateway code:

@UsePipes(new ValidationPipe({ transform: true, enableDebugMessages: true }))
@WebSocketGateway({
  cors: {
    origin: '*',
  },
  //path: '/test',
})
export class TimespanGateway {
  @WebSocketServer() server: Server;

  constructor(private readonly timespanService: TimespanService) {}

  @SubscribeMessage('createTimespan')
  //@UseFilters(new AllExceptionsFilter())
  @UseFilters(new BaseWsExceptionFilter())
  async create(
    @MessageBody() createTimespanDto: CreateTimespanDto,
  ): Promise<{ Message: string; createTimespanDto: CreateTimespanDto }> {
    //return this.timespanService.create(createTimespanDto);
    this.server.emit(JSON.stringify({ message: 'test', createTimespanDto }));
    return { Message: 'From Create', createTimespanDto };
  }

Error captured on server:

[Nest] 7877  - 04/06/2023, 12:31:33 AM   ERROR [WsExceptionsHandler] Bad Request Exception
BadRequestException: Bad Request Exception
    at ValidationPipe.exceptionFactory (/root/fbpTest/janus-ws/node_modules/@nestjs/common/pipes/validation.pipe.js:99:20)
    at ValidationPipe.transform (/root/fbpTest/janus-ws/node_modules/@nestjs/common/pipes/validation.pipe.js:72:30)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at resolveParamValue (/root/fbpTest/janus-ws/node_modules/@nestjs/websockets/context/ws-context-creator.js:105:31)
    at async Promise.all (index 0)
    at pipesFn (/root/fbpTest/janus-ws/node_modules/@nestjs/websockets/context/ws-context-creator.js:107:13)
    at /root/fbpTest/janus-ws/node_modules/@nestjs/websockets/context/ws-context-creator.js:42:17
    at TimespanGateway.<anonymous> (/root/fbpTest/janus-ws/node_modules/@nestjs/websockets/context/ws-proxy.js:11:32)
    at WebSocketsController.pickResult (/root/fbpTest/janus-ws/node_modules/@nestjs/websockets/web-sockets-controller.js:96:24)

Minimum reproduction code

https://github.com/dieeisenefaust/janus-ws

Steps to reproduce

npm run build
npm run start

either use something like Browser WebSocket Client (a Chrome extensions) and/or a HTML client like 'clienttest.html' in the root of the repo provided.

The validation pipe is enforcing the IsNotEmpty class-validator decorators in the create-timespan.dto.ts file. So any message should include userId, location, job and task.

Sending a payload without userId will trigger an exception.

{
  "event": "createTimespan",
  "data": {
         "location": "Egypt",
         "job": "Archeologist",
         "task": "dig"
      }
}

The exception will only show in console.log within your code editor running the server.

Expected behavior

Expect an error to be returned to the client.

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

No response

NestJS version

9.3.0

Packages versions

  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/platform-ws": "^9.3.12",
    "@nestjs/websockets": "^9.3.12",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "ws": "^8.13.0"
  }

Node.js version

18.14.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@dieeisenefaust dieeisenefaust added the needs triage This issue has not been looked into label Apr 6, 2023
@kamilmysliwiec
Copy link
Member

Duplicate #9056

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants