[PATCH] Exception: fix exception details when not specified

From: Royce Lv <lvroyce@linux.vnet.ibm.com> Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation. Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message) class UrlSubNode(object): diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value) - return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % args class NotFoundError(KimchiException): -- 1.8.3.2

On 05/15/2014 07:23 PM, lvroyce@linux.vnet.ibm.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message) you has pass the e.message as code.
you can define a new code such as ""or set code as empty. "KCHAPI0008E": _("API parameter error: %(err)s"), or if you do not want to translation: "KCHAPI0008E": _("%(err)s"), or "KCHAPI0008E": _("%(err)s"),
class UrlSubNode(object):
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value)
- return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % arg
why do you need to change this code? It seems several patches to improve this code.
class NotFoundError(KimchiException):
-- Thanks and best regards! Sheldon Feng(冯少合)<shaohef@linux.vnet.ibm.com> IBM Linux Technology Center

On 05/15/2014 10:42 PM, Sheldon wrote:
On 05/15/2014 07:23 PM, lvroyce@linux.vnet.ibm.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message) you has pass the e.message as code.
you can define a new code such as ""or set code as empty. you can set code as empty or define a new code such as: raise InvalidParameter( "KCHAPI0008E", {"err": e.message}) "KCHAPI0008E": _("API parameter error: %(err)s"),
or if you do not want to translation:
raise InvalidParameter( "KCHAPI0008E", {"err": e.message})
"KCHAPI0008E": _("%(err)s"), or raise InvalidParameter( "", {"err": e.message}) "": _("%(err)s"),
class UrlSubNode(object):
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value)
- return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % arg
why do you need to change this code? It seems several patches to improve this code.
class NotFoundError(KimchiException):
-- Thanks and best regards! Sheldon Feng(冯少合)<shaohef@linux.vnet.ibm.com> IBM Linux Technology Center

On 05/15/2014 10:42 PM, Sheldon wrote:
On 05/15/2014 07:23 PM, lvroyce@linux.vnet.ibm.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message) you has pass the e.message as code.
you can define a new code such as ""or set code as empty.
"KCHAPI0008E": _("API parameter error: %(err)s"),
or if you do not want to translation: "KCHAPI0008E": _("%(err)s"), or "KCHAPI0008E": _("%(err)s"), Good point, ACK
class UrlSubNode(object):
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value)
- return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % arg
why do you need to change this code? It seems several patches to improve this code. Because when gettext returns unicode, the decode will raise error: TypeError: decoding Unicode is not supportd That is what things are like when json schema passing error occurs. REF: http://stackoverflow.com/questions/7634715/python-decoding-unicode-is-not-su...
class NotFoundError(KimchiException):

on 2014/05/15 19:23, lvroyce@linux.vnet.ibm.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message)
class UrlSubNode(object):
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value)
- return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % args
Could you explain this change? I don't see obvious significant differences between the old and new code.
class NotFoundError(KimchiException):
-- Zhou Zheng Sheng / 周征晟 E-mail: zhshzhou@linux.vnet.ibm.com Telephone: 86-10-82454397

On 05/16/2014 09:59 AM, Zhou Zheng Sheng wrote:
on 2014/05/15 19:23, lvroyce@linux.vnet.ibm.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message)
class UrlSubNode(object):
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value)
- return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % args Could you explain this change? I don't see obvious significant differences between the old and new code. Because when gettext returns unicode, the decode will raise error: TypeError: decoding Unicode is not supportd That is what things are like when json schema passing error occurs. REF: http://stackoverflow.com/questions/7634715/python-decoding-unicode-is-not-su...
class NotFoundError(KimchiException):

于 2014年05月21日 10:31, Royce Lv 写道:
On 05/16/2014 09:59 AM, Zhou Zheng Sheng wrote:
on 2014/05/15 19:23, lvroyce@linux.vnet.ibm.com wrote:
From: Royce Lv <lvroyce@linux.vnet.ibm.com>
Not every validation error needs to translate for user, we define user interested errors, other developer interested error should report directly by schema validator, and handled by developer directly. fix exception handling for this situation.
Signed-off-by: Royce Lv <lvroyce@linux.vnet.ibm.com> Signed-off-by: Zhou Zheng Sheng <zhshzhou@linux.vnet.ibm.com> --- src/kimchi/control/utils.py | 6 ++++-- src/kimchi/exception.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
diff --git a/src/kimchi/control/utils.py b/src/kimchi/control/utils.py index 5e63816..a67f627 100644 --- a/src/kimchi/control/utils.py +++ b/src/kimchi/control/utils.py @@ -98,8 +98,10 @@ def validate_params(params, instance, action): try: validator.validate(request) except ValidationError, e: - raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) - + if e.schema.get('error'): + raise InvalidParameter(e.schema['error'], {'value': str(e.instance)}) + else: + raise InvalidParameter(e.message)
class UrlSubNode(object):
diff --git a/src/kimchi/exception.py b/src/kimchi/exception.py index fcf60cc..5a292e0 100644 --- a/src/kimchi/exception.py +++ b/src/kimchi/exception.py @@ -62,7 +62,7 @@ class KimchiException(Exception): # itself to a unicode string. args[key] = unicode(value)
- return unicode(translation.gettext(text), 'utf-8') % args + return translation.gettext(text).encode('utf-8') % args Could you explain this change? I don't see obvious significant differences between the old and new code. Because when gettext returns unicode, the decode will raise error: TypeError: decoding Unicode is not supportd That is what things are like when json schema passing error occurs. REF: http://stackoverflow.com/questions/7634715/python-decoding-unicode-is-not-su...
Ah yes. But why previous it worked? Is it triggered by this patch? Will this code cause exception when gettext returns utf-8 string? Does gettext make a promise on the encoding of the returned string? The args are in unicode, do we need to translation.gettext(text).encode('utf-8').decode('utf-8') % args ?
class NotFoundError(KimchiException):
-- Zhou Zheng Sheng / 周征晟 E-mail: zhshzhou@linux.vnet.ibm.com Telephone: 86-10-82454397
participants (5)
-
Aline Manera
-
lvroyce@linux.vnet.ibm.com
-
Royce Lv
-
Sheldon
-
Zhou Zheng Sheng