Vault API Exception Handling

If you are having a problem using Vault, post a message here.

Moderator: SourceGear

Post Reply
awright
Posts: 3
Joined: Fri Dec 20, 2013 11:16 am

Vault API Exception Handling

Post by awright » Fri Dec 20, 2013 11:23 am

I'm starting to use the Vault API for a project, and I'm wondering if there is a better way to handle exceptions than what I'm doing. I'm using the Vault Standard 6.1 API, and C# 4.0. Currently, it seems that all exceptions coming out of the API are of type System.Exception, instead of something specific to the kind of error that occurred. This is sufficient for printing out error messages to a user, but I can't programmatically distinguish different kinds of exceptions and handle each one differently without (gross) code like this:

Code: Select all

catch (Exception e)
{
    if (e.Message == "The username or password supplied is invalid.")
    {
        // handle invalid credentials
    }
}
Is there something I'm missing, or is this the best approach?

jclausius
Posts: 3706
Joined: Tue Dec 16, 2003 1:17 pm
Location: SourceGear
Contact:

Re: Vault API Exception Handling

Post by jclausius » Fri Dec 20, 2013 3:50 pm

The API doesn't have the most elegant exception handling, and for uniformity, most client/server exceptions will be by a general exception handler.

Try something like this to see if it sheds some more light on the issue:

Code: Select all

try { ... }
catch (Exception ex)
{
   int nStatusCode = VaultConnection.GetSoapExceptionStatusCodeInt(ex);
   if (VaultLib.VaultStatusCodeFailNotValidLogin == nStatusCode) { System.Console.WriteLine("invalid login: " + VaultLib.GetSoapExceptionMessage(nStatusCode)); }
   else if (nStatusCode != -1) { System.Console.WriteLine("Error was: " + VaultLib.GetSoapExceptionMessage(nStatusCode)); }
   else /* nStatusCode == -1 */ { System.Console.WriteLine("This is not a Vault Protocol exception : " + ex.Message); }
}
Jeff Clausius
SourceGear

Post Reply