jsgoupil/quickbooks-sync

Assocating exceptions with tickets

mscappini opened this issue · 2 comments

Just filing an issue for a future issue I will address in a PR.

Currently, when an exception occurs in SendRequestXML or ReceiveRequestXML, exceptions are caught and forwarded to an overridable method, but neglect to pass in the ticket if one was found or not. This makes it difficult to track errors by tickets when Web Connector comes knocking for the last error by ticket. I propose there should be a way to catch exceptions after the ticket is retrieved, then pass that ticket to the exception handling.

Solution 1

try
{
    var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);

    try
    {
        ...
    }
    catch (Exception ex)
    {
        OnException(authenticatedTicket, ex);
    }
}
catch (Exception ex)
{
    OnException(null, ex);
}

Soltution 2

public class QbSyncException : Exception
{
    public QbSyncException(AuthenticatedTicket ticket, Exception innerException)
        : base(null, innerException)
    {
        this.Ticket = ticket;
    }

    public AuthenticatedTicket Ticket { get; private set; }
}

try
{
    var authenticatedTicket = authenticator.GetAuthenticationFromTicket(ticket);

    try
    {
        ...
    }
    catch (Exception ex)
    {
        throw new QbSyncException(authenticatedTicket, ex);
    }
}
catch (QbSyncException ex)
{
    OnException(ex.Ticket, ex);
}
catch (Exception ex)
{
    OnException(null, ex);
}

I'm leaning towards solution 2. Seems cleaner to handle exception logic in the set of catches.

That's a great idea. I like solution 2 as well!