diff --git a/db/index.ts b/db/index.ts index 16fb582..e21dd67 100644 --- a/db/index.ts +++ b/db/index.ts @@ -61,7 +61,12 @@ export async function createBooking({ const bookedDays = await getBookedDays() if (booking.days.some((day: string) => bookedDays.includes(day))) { - throw new mongoose.Error.ValidationError(booking) + const error = new mongoose.Error.ValidationError(booking) + error.addError( + 'endDate', + 'At least on one day the van is not avaliable. Choose another startDate or endDate.' + ) + throw error } let booker = await Booker.findOne({ email }).exec() diff --git a/pages/api/booking/index.tsx b/pages/api/booking/index.tsx index 733c01b..702969e 100644 --- a/pages/api/booking/index.tsx +++ b/pages/api/booking/index.tsx @@ -19,9 +19,8 @@ export default async function userHandler( res.status(200).json(booking) } catch (e) { console.error(e) - - if (e instanceof Error.ValidatorError) { - res.status(400).end(e.message) + if (e instanceof Error.ValidationError) { + res.status(400).json({ message: e.message, errors: e.errors }) return } res.status(500).end(`Internal Server Error...Guru is meditating...`)