#ERROR [RpcExceptionsHandler] Error

9 messages · Page 1 of 1 (latest)

chilly walrus
#

Method in service.

async loginUser(dto: LoginDto) {
    const user = await this.usersRepository.findOne({ where: { uuid: dto.uuid } });
    if (!user) {
      throw new NotFoundUserException();
    }

    if (!await user.validatePassword(dto.password)) {
      await this.historyService.create({ uuid: dto.uuid, ip: dto.ip }, HistoryType.loginFailure);
      throw new IncorrectPasswordException();
    }

    await this.historyService.create({ uuid: dto.uuid, ip: dto.ip }, HistoryType.loginSuccess);

    return user;
  }
#

NotFoundUserException and IncorrectPasswordException. ```ts
export class NotFoundUserException extends Error {}
export class IncorrectPasswordException extends Error {}

boreal wolf
#

Nest won't deserialize to your custom errors by default. You'll need to tell it how to handle that. It'll just get sent back over the wire as an Error and the underlying transport (grpc in this case) will handle it how it normally does

#

Think about it, if your service responded to a C# gateway, it would just know it as an Error, not a Typescript specific IncorrectPasswordException. They're separate servers that communicate with each other. That doesn't mean they immediately know how to deserialize into the "correct" Error class

chilly walrus
#

And I've made it so that for services, I use custom errors. And for controllers I use GRPC/HTTP errors.

boreal wolf
#

It might have the grpc codes, but it'll still just be seen as an Error in the gateway. You'd have to set up special deserialization rules

chilly walrus
# boreal wolf It might have the grpc codes, but it'll still just be seen as an Error in the ga...

I don't know what I'm talking about or what to do. I open https://docs.nestjs.com/techniques/serialization

#

And i don't understnad how this is relevant to my problem.