On 09/05/2012 03:42 PM, Petr Viktorin wrote:
On 09/05/2012 03:19 PM, Tomas Babej wrote:
Hi,

User-unfriendly errors were caused by re-raising errors
from external python module netaddr.

https://fedorahosted.org/freeipa/ticket/2588

Tomas


_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel


I don't agree with this approach. Raising another module's errors in our code is ugly, and will break if netaddr changes. The arguments to pass to the exceptions are undocumented (see http://packages.python.org/netaddr/api.html#custom-exceptions). The wording of error messages in libraries can usually change at any time, and is intended for developers, not end users.

This should be either fixed upstream (unlikely, using the repr() of the argument is a sane thing to do at their side), or we should pass bytestrings to netaddr (a possible quick fix, not sure it it'll work), or, ideally, we should raise IPA's own errors.

Well, this particular fix wouldn't have broken anything, since it was raising the same error that the except clause in which the raising occured caught. However, I changed this to StandardError, since the error message is extracted and packed into ValidationError during further validation and therefore simple format message is suitable.

Tomas
>From e39ef6e03cff06869649fa190b7a19211d886aa3 Mon Sep 17 00:00:00 2001
From: Tomas Babej <tba...@redhat.com>
Date: Wed, 5 Sep 2012 09:03:18 -0400
Subject: [PATCH] Remove user-unfriendly "u" character from error messages

User-unfriendly errors were caused by re-raising errors
from external python module netaddr.

https://fedorahosted.org/freeipa/ticket/2588
---
 ipapython/ipautil.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/ipapython/ipautil.py b/ipapython/ipautil.py
index a212aa6efc02023d323ba196e9e93bc6c49a1129..f7e0a0cd0ffb7b874ac74240e828350508e9f268 100644
--- a/ipapython/ipautil.py
+++ b/ipapython/ipautil.py
@@ -50,6 +50,7 @@ from ipapython.ipa_log_manager import *
 from ipapython import ipavalidate
 from ipapython import config
 from ipapython.dn import DN
+from ipalib import errors
 
 try:
     from subprocess import CalledProcessError
@@ -105,18 +106,19 @@ class CheckedIPAddress(netaddr.IPAddress):
             try:
                 try:
                     addr = netaddr.IPAddress(addr, flags=self.netaddr_ip_flags)
-                except netaddr.AddrFormatError:
+                except netaddr.AddrFormatError as e:
                     # netaddr.IPAddress doesn't handle zone indices in textual
                     # IPv6 addresses. Try removing zone index and parse the
                     # address again.
+                    msg = e.message.replace(" u'"," '") # remove the unicode sign
                     if not isinstance(addr, basestring):
-                        raise
+                        raise StandardError(msg)
                     addr, sep, foo = addr.partition('%')
                     if sep != '%':
-                        raise
+                        raise StandardError(msg)
                     addr = netaddr.IPAddress(addr, flags=self.netaddr_ip_flags)
                     if addr.version != 6:
-                        raise
+                        raise StandardError(msg)
             except ValueError:
                 net = netaddr.IPNetwork(addr, flags=self.netaddr_ip_flags)
                 if not parse_netmask:
-- 
1.7.11.4

_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to