keystonejs/keystone — pull request #660 by ianbjorndilling (2014-10-03 20:06:54+00:00)
Title: Custom deletion hook error messages: issue #536

I looked into issue #536.

Errors were showing up correctly with 'save' hooks because non autocreated saves are handled by the updateHandler, which will display a flash message with the error message so long as the flasherrors option is set to true.

If there is a remove, item.remove() is called in the list route controller. The callback did not add the error's message into the flash message.

I made a few tweaks so that the error message is added into the flash message. I also made changes to the list api so that an error message is sent in the response json. Then I made sure the message was properly displayed in the alert box when the trashcan icon was clicked.

Here's a quick example:

-- (in User.js) --
User.schema.pre('remove', function(next) {
return next(new Error("Deleting users is not possible."));
});

-- (in browser) --
![working fix](https://cloud.githubusercontent.com/assets/7054089/4511759/ab3ee56c-4b38-11e4-88ea-6e9a66d157eb.png)