You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm looking for help on how to customize error responses triggered during validation in Payload CMS.
Instead of returning my custom error, the API always responds with a static 500 Internal Server Error.
My Case:
I have a purchasedTickets field where I store purchased tickets as a JSON array. Before saving a new purchase, I check if any of the newTickets already exist in purchasedTickets. If they do, I want to prevent the update and return a structured error response.
Here’s my current beforeValidate hook: hooks: {beforeValidate: [ async ({ data, originalDoc, req }) => { if (data.newTickets) { const duplicateTickets = data.newTickets.filter((ticket) => originalDoc.purchasedTickets.some( (existingTicket) => existingTicket.ticket === ticket.ticket ) ); if (duplicateTickets.length > 0) { console.log("❌ Duplicate tickets detected:", duplicateTickets); if (req?.res) { req.res.status(400).json({ errors: [ { message: "Some of the new tickets already exist in the purchased tickets." } ], }); } throw new Error( "Some of the new tickets already exist in the purchased tickets Error 84." ); } } }, ], }
Issues I'm Facing:
1️⃣ req.res is undefined – I tried sending a response manually, but req.res does not exist in Payload CMS requests.
2️⃣ Payload CMS still returns a 500 Internal Server Error instead of my structured error message.
3️⃣ Ideally, I want to return a 400 Bad Request with a JSON response like this: { "errorCode": 400, "message": "Some of the new tickets already exist in the purchased tickets." }
4️⃣ If beforeValidate isn't the right place, what hook should I use to properly stop the update and send a custom response?
Questions:
How can I correctly stop the update and return a custom error response in Payload CMS?
Is there a better way to handle validation errors without getting a generic 500 error?
Would love any guidance from the community! 🚀 Thanks in advance! 🙌🏼
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Howdy everyone! 👋🏼
I'm looking for help on how to customize error responses triggered during validation in Payload CMS.
Instead of returning my custom error, the API always responds with a static 500 Internal Server Error.
My Case:
I have a purchasedTickets field where I store purchased tickets as a JSON array. Before saving a new purchase, I check if any of the newTickets already exist in purchasedTickets. If they do, I want to prevent the update and return a structured error response.
fields: [ { name: "purchasedTickets", type: "json", defaultValue: [], }, ],
Here’s my current beforeValidate hook:
hooks: {beforeValidate: [ async ({ data, originalDoc, req }) => { if (data.newTickets) { const duplicateTickets = data.newTickets.filter((ticket) => originalDoc.purchasedTickets.some( (existingTicket) => existingTicket.ticket === ticket.ticket ) ); if (duplicateTickets.length > 0) { console.log("❌ Duplicate tickets detected:", duplicateTickets); if (req?.res) { req.res.status(400).json({ errors: [ { message: "Some of the new tickets already exist in the purchased tickets." } ], }); } throw new Error( "Some of the new tickets already exist in the purchased tickets Error 84." ); } } }, ], }
Issues I'm Facing:
1️⃣ req.res is undefined – I tried sending a response manually, but req.res does not exist in Payload CMS requests.
2️⃣ Payload CMS still returns a 500 Internal Server Error instead of my structured error message.
3️⃣ Ideally, I want to return a 400 Bad Request with a JSON response like this:
{ "errorCode": 400, "message": "Some of the new tickets already exist in the purchased tickets." }
4️⃣ If beforeValidate isn't the right place, what hook should I use to properly stop the update and send a custom response?
Questions:
Would love any guidance from the community! 🚀 Thanks in advance! 🙌🏼
Beta Was this translation helpful? Give feedback.
All reactions