vendredi 2 septembre 2016

Throwing business exceptions

To invoke external services such as payment gateways I create a provider assembly such as: MyCompany.Providers.Braintree, which encapsulate all the logic related to making the service invocation. It is works basically as pass through and it is contained in is own assembly.

Let's say the provider has a "DoPayment" method which invokes a service on the gateway which returns a couple of status as well as a body message:

  • code 111 -> Payment was successfully
  • code 222 -> Not enough funds
  • code 333 -> Unknown payee

Should the provider throw a "business" exception when the payment has not completed successfully, or should the service basically return a type which wraps what the gateway return?

I have always opted out for not throwing exceptions in these kind of situations because basically this is not as exception case - the gateway processed the request successfully (the request was processed even thought the payment was not completed) and that what MS says as well:

Creating and Throwing Exceptions (C# Programming Guide)

Exceptions should not be used to change the flow of a program as part of ordinary execution. Exceptions should only be used to report and handle error conditions.

What do you guys do?

Aucun commentaire:

Enregistrer un commentaire