Errors and Restrictions

Update:  A better version of the below code has been provied in another article.  The new code gets both the error code and any restriction codes.


Errors

You are probably reading the error code incorrectly.  I'm guessing that you are getting it from the Message text of an Exception that gets thrown when you make an API call.  Although the Message usually contains the code, it's not the "correct" place to get it. 

The correct thing to do is catch the SoapException, then check the Details property, which is a bunch of XML data.  The Vault error code can be found between the <sl:errorcode> tags. Also, some errors will be accompanied by extra information in the form of a string array, found between the <sl:param> tags. Each parameter will have its own tag and "pid", which is an index value starting at 1. Most errors will not have extra parameters.  See the Error Codes page in the API documentation for more information.

If you don't get a SoapException, it means that the error is not coming from the Vault server.  Something else is causing the error, such as a network error.


Restrictions

Restrictions can be thought of as extended error information.  In the case of an Exception from the Vault server, there is one and only one error code.  Restrictions, however, can have multiple codes.  This is useful for complex operations where multiple things can fail at once, and you want to display everything to the user in a single dialog.

If the error code is 1092, 1387, or 1633, then you need to examine the restriction information to find out what the problem is.  For all other errors, there is no restriction information.

Just like with errors, you need to examine the Details XML in the Soap Exception.  The Restriction Codes page in the API documentation has more information.


Sample Code

Here is the correct way to read the error code.

C#:

public static string GetErrorCodeString(Exception e)
{
    SoapException se = e as SoapException;
    if (se != null)
    {
        try
        {
            return se.Detail["sl:sldetail"]["sl:errorcode"].InnerText.Trim();
        }
        catch
        { }
    }
    return null;
}

 

VB:

Public Shared Function GetErrorCodeString(ByVal e As Exception) As String
    If (TypeOf e Is SoapException) Then
        Dim se As SoapException = e
        Try
            Return se.Detail("sl:sldetail")("sl:errorcode").InnerText.Trim()
        Catch
        End Try
    End If
    Return Nothing
End Function


Comments

7 responses to “Errors and Restrictions”

  1. Technically, you should use If se IsNot Nothing which is a better translation of se != null. When compiled, Not se Is Nothing is not optimized as you would expect.

  2. Thanks for the tip. However, I found another issue with the original code VB code, so I decided to rewrite it. In the old code “Dim se As SoapException = e” would cause a cast exception if e wasn’t a SoapException.
    As you can tell, I’m more of a C# guy.

  3. Nothing wrong with being a c# guy – I float between both every day :)

  4. Mike Beebe Avatar
    Mike Beebe

    What library reference SoapException? My code doesn’t know what this is.

  5. System.Web.Services.Protocols.SoapException is the full class name.

  6. Shane Cossey Avatar
    Shane Cossey

    This is just returning “1387” for me – how do I find out about the restriction information?

  7. Follow the link at the top of the article.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading