jsgoupil/quickbooks-sync

Handle StatusCode error better

jsgoupil opened this issue · 1 comments

Currently, in the ExecuteResponse we check if the main required object is present. But when there is an error, it ends up to be an ugly if case for each status error code.

Example with InvoiceAdd

if (response.InvoiceRet != null) { ... }
else if (response.statusCode == "3120") {

}

Not only this is ugly, but when the response is coming back with an error, there are no state saved properly with what has been sent and what is being returned.
So I end up doing either a save to the database or something ugly like:

                // If the object cannot be added, it's because the parent has been deleted.
                ////"Object "80000027-1429563689" specified in the request cannot be found.  QuickBooks error message: Invalid argument.  The specified record does not exist in the list."
                var startsWith = "Object \"";
                var endsWith = "\" specified in the request cannot be found.  QuickBooks error message: Invalid argument.  The specified record does not exist in the list.";
                if (response.statusMessage.StartsWith(startsWith) && response.statusMessage.EndsWith(endsWith))
                {
                    // We will remove the entity sync, we can't sync this invoice. We will also remove the site.
                    var invoiceIdStr = messageService.RetrieveMessage(authenticatedTicket.Ticket, GetName(), Key);
                    int invoiceId = 0;
                    if (invoiceIdStr != null && int.TryParse(invoiceIdStr, out invoiceId))
                    {

Do you think maybe we should make a new method that is called if the status code is not OK?