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?)

2 Answers 2


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.

  • I agree. Ideally, you shouldn't ever be put in such a situation due to client-side validation. So if something like that does occur, security, not convenience, is top priority.
    – Neil
    Jun 9, 2015 at 10:39
  • Isn't the self contained single action dependent on the actual intended behavior ? If they need to be all saved at once then fail the whole action if they don't then just send what failed or succeeded. For example I want to upload a bunch of files to my dropbox, should dropboxes api make me re-upload my files if one of them fails to save ?
    – pllee
    Dec 22, 2015 at 17:30
  • I would expect a file upload api to require one request per file. I don't know dropbox's api, but a quick skim seems to indicate that it supports one or multiple requests for a single file, but not one request for multiple files.
    – JamesT
    Dec 22, 2015 at 19:29
  • I was just using it as an example, not what they actually support. Say my users upload thousands of files at a time and I want to reduce chattiness by making it a bulk endpoint (and they can't zip for whatever reason). They shouldn't have to re-upload everything if the last one failed right? (in this case the files are independent of each other) That would be a bad user experience. My point being the behavior really all depends on the use case. I don't think it is a hard and fast rule like your answer implies. I would like to hear an argument of why it should always fail as a whole.
    – pllee
    Dec 22, 2015 at 20:33
  • There are no hard and fast rules. There are good rules of thumb and I'm sure you could find an edge case where any one of them doesn't apply. The OP asked for an array of objects and, crucially, best practice. If you feel you have a good case for ignoring it, then crack on chap. You, presumably, know your requirements better than I do.
    – JamesT
    Dec 22, 2015 at 20:50

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.