nestjs icon indicating copy to clipboard operation
nestjs copied to clipboard

Request: class validator, promises response errors

Open bmcgoncalves opened this issue 3 years ago • 8 comments

Hi, we want to create an functions ecosystem with rabbitmq. Something like this:

One queue per function: image

All validations need to pass from API to Microservices: image

To do this we need class validator on microservices level and we need to return promises with error messages. With this type of solution anyone with a connection to rabbitmq can use any function.

bmcgoncalves avatar Mar 19 '21 14:03 bmcgoncalves

@bmcgoncalves So what you are looking for is to use class-validator and provide custom errors right? You can do that easily by creating a simple interceptor that uses class-validator and you can get errors and format the way you need but it would also imply that you create a custom (generic) payload, something like:

{
  success: boolean,
  data?: T
  error?: T
}

That is a naive solution but you need to handle all the success/error logic by yourself, Nestjs or Golevelup don't imply any payload structure and they leave it up to the implementation I'm not sure if this was what you were looking for but i hope this helps you

underfisk avatar Mar 19 '21 15:03 underfisk

I know I can do that, thanks. But i think it could be integrated in your project. For instance, there is another project for rabbitmq that supports a @Validade decorator, and we can reject promises to the client (we can catch an error). That project has only one problem: only supports one queue for multiple routing keys.

I think I need a merge of the two projects ;)

bmcgoncalves avatar Mar 19 '21 23:03 bmcgoncalves

@bmcgoncalves I have discussed that idea but it would imply having a custom payload, i might have to talk with @WonderPanda and open a suggestion for the whole community that uses already this package but its a nice suggestion

underfisk avatar Mar 19 '21 23:03 underfisk

@bmcgoncalves Thanks for the suggestion, this is definitely something that could be improved. Which is the other library that you're referring to which provides this behavior?

WonderPanda avatar Mar 26 '21 19:03 WonderPanda

Hi, I haven't tried the package. From the examples and description it seems to do what I want but only for one queue per microservice.

https://www.npmjs.com/package/nestjs-rmq

bmcgoncalves avatar Mar 26 '21 19:03 bmcgoncalves

@bmcgoncalves Thanks, I appreciate the feedback. I will definitely consider this for functionality in the next major version of the library

WonderPanda avatar Mar 26 '21 20:03 WonderPanda

Hi, is there any updates from this issue? We are switching from NestJS message pattern to this lib and we already have functionality to handle errors thrown from promises, so it would be great to see this implemented, it will save us a lot of time migrating.

narekkeryan avatar Apr 01 '22 13:04 narekkeryan

Same for us, we would also appreciate to be able to benefit from throwing errors from promises.

L-U-C-K-Y avatar Apr 01 '22 16:04 L-U-C-K-Y

@WonderPanda @underfisk would you have a suggested custom payload that I could try to make a PR?

L-U-C-K-Y avatar Oct 10 '22 16:10 L-U-C-K-Y

@L-U-C-K-Y It's hard to say at this point if we want to implement such a thing, for now, you are able to intercept the error messages and return whatever payload you wish to. In order to integrate that into the library we would need some sort of "opt-in" flag or a way to disable if the users do not want that behaviour by default because every product has different requirements

underfisk avatar Oct 10 '22 16:10 underfisk