Add field - detect and deal with duplicate entries

Am using the Add Field (Dynamic Component) , to add an instance of a business object.
When I add a duplicate value, I get a 500 errors (because indeed it’s a duplicate row in the table underlying the business object).
I don’t see anything in the implementation that shows how to catch errors…
So how do I catch this and give an appropriate message to the user?

I assumed that an ‘error’ attribute triggers somemethod($event) would be the way to address this, but at least with the Add Field, that method doesn’t appear to be driven…

The first step (which it sounds like you have) is to make sure you have some kind of unique constraint on the business object that defines uniqueness. This ensures data integrity but by itself does not give a good user experience. That is because it causes a 500 error during the insert and the details of it are not returned to the client because you don’t want to expose the internal data structure.

Here are a few patterns that you can use on top of the constraint to give good usability.

The best solution is to check for duplicate names before you actually attempt to create the item. If you are controlling the create, you could just do a query before the create asking for a count of items that match the properties. If you find one, you can display an error to the user.

A variation of this would be to add a custom async validator to the name form control that does an API call to check for a duplicate. This gives the best user experience but is also a little more technical to implement.

The third way is to do a check on the server side before create. You can create an event handler for the before create event. In that handler, you would do a find operation to see if the item already exists. If it does exist, you can

 throw {statusCode: 422, message: 'A blah with name yada already exists'}

That will be passed to the client and the user will see the message.

but if the create is being controlled by the ‘Add Field’ component, what’s the right approach in that case?
Or should I switch to controlling the add myself (certainly do-able)

Option 3 (server side before create) should work with the Add field.