pythonsvnpysvn

How do I correctly catch pySVN exceptions?


I have a function that connects to an SVN database and checkouts a folder/file. When given the correct login info, it proceeds as it should. However, when given the wrong login information, pysvn raises an exception and crashes. I'm trying to catch and handle the error, but I can't seem to correctly catch it:

Code (assuming 'svnlogin' contains garbage info):

def connect_and_checkout():
    client = pysvn.Client()
    client.callback_get_login = svnlogin

    try:
        client.checkout(svnurl(),
        './examples/pysvn')

    except (pysvn.ClientError) as e:
        print("error during svn command", e)    

Expected output:

error during svn command, 'error'

Actual output (crashes the program):

svn: E235000: In file 'c:\buildroot\win64-vc-14.1-1.14.0\subversion-1.14.0\subversion\libsvn_client\checkout.c' line 88: assertion failed (svn_uri_is_canonical(url, scratch_pool))

Besides "pysvn.ClientError" as the exception parameter, I've also tried "svn", "pysvn.svn", "AssertionError", "SvnException" and just a blank "except:". None of them seems to correctly catch the exception and let me handle it.


Solution

  • Turns out this is a bug in pySVN and will be fixed in revision 2047 according to the developer Barry Scott. See the following bug report for more information

    The bug occurs when the underlying SVN-connector finds an invalid URL, for an example from a bad user-input. The bug does not occur if the URL itself is valid, but the connection fails.

    In the meantime, either make some input validation that checks if "www." etc are present, or use the following snippet to manually raise an exception which can be handled:

    c = pysvn.Client()
    if not c.is_url( url_candicate ):
        raise AttributeError( 'bad url' )