microsoft / PowerPlatform-DataverseServiceClient

Code Replica for Microsoft.PowerPlatform.Dataverse.Client and supporting nuget packages.
MIT License
284 stars 52 forks source link

Inconsistent exception behavior #470

Closed menerth closed 1 month ago

menerth commented 1 month ago

Hi guys.

With my team we have recently encountered a surprising difference regarding sending requests via ServiceClient.

There are currently at least 2 ways one can execute a request:

The difference is that Execute/ExecuteAsync throws an exception in case something went wrong, however ExecuteOrganizationRequestAsync catches the exception and returns null.

Is this intended behavior for those more tailored methods?

await _serviceClient.ExecuteAsync(request); // throws an exception

await _serviceClient.ExecuteOrganizationRequestAsync(request) // returns null

ServiceClient uses default configs

MattB-msft commented 1 month ago

This is by design to support backward compatibility with CrmServiceClient, CrmServiceClient used what was known as the C++ error pattern, which means a Null result and a check against "last error". this is a nod to how long CrmServiceClient has been around.

Using **ExecuteOrganziationRequest***, if it returns null, you can check "LastError" and "LastException" to get the last failure.

Using the base IOrganizationService Handler, we follow the 'normal' exception throw pattern you see in .net.

In a future version we may expose the control option to pop exceptions out of ExecuteOrganizationRequest*

thanks.