Customize Rest API response in collection hooks #5691
-
Hey I'm new to Payload CMS, so I'm trying to implement a comment functionality to a 'comments' collection. Basically everyone is allowed to comment without logging in. So I'd like to add an additional validation: if any user posted a comment within a time interval, the server should respond with an error code to the client side. I'm stuck on trying to modify the Rest API response in collection hooks. Could someone show me the direction on how to make this work? Thank you! 👏
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hey @rk54rk, You can import APIError from 'payload/errors' and instead of return Promise.reject, you'd have There are other Error types you can choose from if you wish also. I hope that answers your question, let me know if you need anything else! |
Beta Was this translation helpful? Give feedback.
-
Thank you Dan this works perfectly! 👍 |
Beta Was this translation helpful? Give feedback.
Hey @rk54rk,
You can import APIError from 'payload/errors' and instead of return Promise.reject, you'd have
throw New APIError(message, 429)
There are other Error types you can choose from if you wish also.
I hope that answers your question, let me know if you need anything else!