x

.NET API and Error Handling

Hello group, I have a quick question about .NET API and how to use the .Errors model.

 

I'm almost done integrating my website with Square to accept online payment but I've come across something I'm not sure how to solve. I'll try to explain as best as I can.

 

Assumptions: I'm using Sandbox mode and my Amount Money = 403 (403 because I just want to generate a Credit Card Declined error for testing purposes) and I have the Charge method wrapped around a Try...Catch as follows:

Try

     ChargeResponse = TransactionApi.Charge(AccessToken, LocationID, Body)
     ...etc etc etc
Catch Ex As Square.Connect.Client.ApiException
     'Save error here blah blah blah
End Try

Capturing the credit card declined error works ok, but the error "comes back" in the Ex (Variable) As Exception:

Ex.Message = Credit Card Declined.
This is ok I guess BUT THE ChargeResponse.Error and ChargeResponse.Transaction models are EMPTY (Nothing, I suppose the Transaction model should be empty as no transaction was generated)

 

So basically I can't say something like: If Not IsNothing(ChargeResponse.Error) Then...

I tried deleting the Try...Catch to check if the Error model gets populated but the execution code exits the function to the previous Try...Catch (the one that called the Function)

 

Is this normal?  it means the Error model nevers gets populated and we have to read all the errors from the Try...Catch Ex variable??

 

Thank youuuuuuuuuuuu   very much

Tags (1)
802 Views
Message 1 of 2
Report
1 REPLY 1
Alumni

Generally, this is the expected behaviour. For some endpoints you will either get a response body back that has either the errors compoent filled up, or the response body. You wouldn't get an error response and body in the response (transactions in this case) because the request failed for the reason described in the error field, and won't be able to return the body. 

792 Views
Message 2 of 2
Report