Catch Errors
When performing API calls, you can wrap within a catch block with an exception handler to easily extract the message returned.
try
{
var model = apiClient.Endpoints.Accounts.Get("10", "ACCTCODE");
}
catch (Exception ex)
{
errorMessage = Ungerboeck.Api.Sdk.Helpers.Errors.GetAPIErrorMessage(ex);
}
LastResponseError
An easy way to track the previous error thrown is to see what is logged in ApiClient.LastResponseError.
Ungerboeck.Api.Models.Errors.ApiErrors errors = apiClient.LastResponseError;
The LastResponseError also contains the ErrorCode of the error, which is generally better for programmatic deciphering of the category of the error. See below for an example:
string errorCode = apiError?.ErrorList?.First()?.ErrorCode
if (errorCode == "SaveResult_MissingRequiredFields"){
//Handle missing fields
}
ThrownErrorIntensity
The SDK allows user options to control what level of error results in a thrown error. See below for the four levels and examples of each.
Normal
SDK throws exceptions on any error, including 404 not found exceptions.
apiClient.GlobalOptions.ThrowErrorIntensity = Ungerboeck.Api.Models.Options.ThrowErrorIntensity.Normal;
var account = apiClient.Endpoints.Accounts.Get("10", "FAKEACCT"); //Assuming FAKEACCT is not a real account code, this will throw an error
UnexpectedClientOrServerErrors
SDK should throw exceptions on any user error but not including 404 GET resource errors. Not found resources return as null.
apiClient.GlobalOptions.ThrowErrorIntensity = Ungerboeck.Api.Models.Options.ThrowErrorIntensity.UnexpectedClientOrServerErrors;
account = apiClient.Endpoints.Accounts.Get("10", "FAKEACCT"); //Assuming FAKEACCT is not a real account code, this will retrieve as null without a thrown error since it's a Not Found (404)
//But an invalid Add will throw an error
var invalidAccount = new AllAccountsModel { Organization="10", Class = Ungerboeck.Api.Models.USISDKConstants.AccountClass.Contact }; //Missing LastName. This will throw an error since it's a Bad Request (400)
apiClient.Endpoints.Accounts.Add(invalidAccount);
UnexpectedServerErrorsOnly
Wrapper should only throw exceptions on internal server errors (500). Exceptions are otherwise only included in the client.LastResponseError info.
apiClient.GlobalOptions.ThrowErrorIntensity = Ungerboeck.Api.Models.Options.ThrowErrorIntensity.UnexpectedServerErrorsOnly;
var account = apiClient.Endpoints.Accounts.Get("10", "FAKEACCT"); //Assuming FAKEACCT is not a real account code, this will retrieve as null without a thrown error since it's a Not Found (404)
//An invalid Add will not throw an error
var invalidAccount = new AllAccountsModel { Organization = "10", Class = Ungerboeck.Api.Models.USISDKConstants.AccountClass.Contact }; //Missing LastName. This will throw an error since it's a Bad Request (400)
apiClient.Endpoints.Accounts.Add(invalidAccount);
//Nothing will throw errors aside from internal server errors (500). You can still see errors programmatically using LastResponseError
return apiClient.LastResponseError;
DoNotThrowErrors
No errors are thrown. Errors are only logged to the Api Client object (ApiClient.LastResponseError). This is if you want to fully rely on LastResponseError for logging errors.
Comments
0 comments
Please sign in to leave a comment.