سؤال

I want to POST an array of objects and make an insertion in database concerning each of them.

What is the best practice if one object does not conform and raise an Exception? (cancel all transactions or just one and specify it when I return this list of objects?)

هل كانت مفيدة؟

المحلول

Your post should be a single, self contained action that succeeds as a whole or fails as a whole. Therefore if one item contains an error you should reject the entire set.

You should also be checking these items if at all possible before even starting your database transaction so you have the chance to error out early.

Of course, if your database engine raises an exception (for example due to referential constraints), then you rollback the whole transaction.

نصائح أخرى

If it's a batch operation, I'd rather process all the operations one by one, and return the array of objects with their own status.

You can return the HTTP status code 200 OK if all the operations succeeded, or a 207 Multi-Status if you have operations that failed amongst your batch.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى softwareengineering.stackexchange
scroll top