samedi 28 avril 2018

How should I handle failure between API calls

I have encounter a problem where a action can be done through either a sequence of existing Http requests like the following:

api1() -> api2() -> api3()

Or I will have to create a new API that does it at once so its something like:

apiNew() {
   api1();
   api2();
   api3();
}

Then I will just send request to apiNew() and let it do all of the three functions.

However, since all three apis involves database change, each individual API is wrapped with a transaction block.

Here is the problem, in case I call them separately. Then if api1() succeed but api2() failed, the database is in some in the middle state which will require manual change, so it can move to the correct state.

The way I am doing it now is to create the apiNew() and wrap all three in the same transaction block so all the database changes are going under the same transaction.

I am just wondering, is there some other way of doing this type of work, so I can reuse api1() api2() and api3() in a way that it does the error handling correctly without rewrite the code to a single API call.

Aucun commentaire:

Enregistrer un commentaire