summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorianb <devnull@localhost>2008-03-26 02:17:04 +0000
committerianb <devnull@localhost>2008-03-26 02:17:04 +0000
commitaed98f824bb54253b9d17ece819e965c532beea4 (patch)
tree7473f3b66650d5ee7bb224d9c939079823937f1f
parent3bd39f08a20e2231fec8496244833ea960603423 (diff)
downloadpaste-aed98f824bb54253b9d17ece819e965c532beea4.tar.gz
Fix #240, unicode exceptions cause collector to fail. Also make tests somewhat less fragile
-rw-r--r--docs/news.txt2
-rw-r--r--paste/exceptions/collector.py14
-rw-r--r--tests/test_exceptions/test_error_middleware.py11
3 files changed, 23 insertions, 4 deletions
diff --git a/docs/news.txt b/docs/news.txt
index d7adcd2..f4c03d6 100644
--- a/docs/news.txt
+++ b/docs/news.txt
@@ -26,6 +26,8 @@ svn trunk
overridden somewhere (e.g., when using errordocuments POST would
show up as GET).
+* Exceptions with unicode messages don't cause the collector to fail.
+
1.6.1
-----
diff --git a/paste/exceptions/collector.py b/paste/exceptions/collector.py
index 1520dd5..65f9ec0 100644
--- a/paste/exceptions/collector.py
+++ b/paste/exceptions/collector.py
@@ -33,6 +33,7 @@ import warnings
DEBUG_EXCEPTION_FORMATTER = True
DEBUG_IDENT_PREFIX = 'E-'
+FALLBACK_ENCODING = 'UTF-8'
__all__ = ['collect_exception', 'ExceptionCollector']
@@ -364,7 +365,7 @@ class ExceptionCollector(object):
frames=frames,
exception_formatted=self.collectExceptionOnly(etype, value),
exception_type=etype,
- exception_value=str(value),
+ exception_value=self.safeStr(value),
identification_code=ident,
date=time.localtime(),
extra_data=extra_data)
@@ -379,6 +380,17 @@ class ExceptionCollector(object):
pass
return result
+ def safeStr(self, obj):
+ try:
+ return str(obj)
+ except UnicodeEncodeError:
+ try:
+ return unicode(obj).encode(FALLBACK_ENCODING, 'replace')
+ except UnicodeEncodeError:
+ # This is when something is really messed up, but this can
+ # happen when the __str__ of an object has to handle unicode
+ return repr(obj)
+
limit = 200
class Bunch(object):
diff --git a/tests/test_exceptions/test_error_middleware.py b/tests/test_exceptions/test_error_middleware.py
index 064f909..a34de73 100644
--- a/tests/test_exceptions/test_error_middleware.py
+++ b/tests/test_exceptions/test_error_middleware.py
@@ -44,6 +44,9 @@ def bad_app():
"No argument list!"
return None
+def unicode_bad_app(environ, start_response):
+ raise ValueError(u"\u1000")
+
def start_response_app(environ, start_response):
"raise error before start_response"
raise ValueError("hi")
@@ -76,20 +79,22 @@ def test_makes_exception():
assert 'paste.lint' in res
assert 'paste.exceptions.errormiddleware' in res
+def test_unicode_exception():
+ res = do_request(unicode_bad_app)
+
+
def test_start_res():
res = do_request(start_response_app)
res = strip_html(str(res))
- #print res
assert 'ValueError: hi' in res
assert 'test_error_middleware' in res
- assert ':49 in start_response_app' in res
+ assert ':52 in start_response_app' in res
def test_after_start():
res = do_request(after_start_response_app, 200)
res = strip_html(str(res))
#print res
assert 'ValueError: error2' in res
- assert ':53' in res
def test_iter_app():
res = do_request(lint.middleware(iter_app), 200)