diff --git a/examples/demo/demo.py b/examples/demo/demo.py index fef5848..ecbce87 100644 --- a/examples/demo/demo.py +++ b/examples/demo/demo.py @@ -1,3 +1,16 @@ +# coding=utf8 +""" +A mini-demo of what wsme can do. + +To run it:: + + python setup.py develop + +Then:: + + paster serve demo.cfg +""" + from webob.dec import wsgify from wsme import * @@ -11,6 +24,10 @@ class DemoRoot(WSRoot): def multiply(self, a, b): return a * b + @expose(unicode) + def helloworld(self): + return u"こんにちは世界 (<- Hello World in Japanese !)" + def app_factory(global_config, **local_conf): return wsgify(DemoRoot()._handle_request) diff --git a/wsme/controller.py b/wsme/controller.py index a480cf1..d066a7e 100644 --- a/wsme/controller.py +++ b/wsme/controller.py @@ -1,12 +1,15 @@ import inspect import traceback import weakref +import logging from wsme import exc from wsme.types import register_type __all__ = ['expose', 'validate', 'WSRoot'] +log = logging.getLogger(__name__) + registered_protocols = {} @@ -107,14 +110,20 @@ class WSRoot(object): def _format_exception(self, excinfo): """Extract informations that can be sent to the client.""" if isinstance(excinfo[1], exc.ClientSideError): - return dict(faultcode="Client", - faultstring=unicode(excinfo[1])) + r = dict(faultcode="Client", + faultstring=unicode(excinfo[1])) + log.warning("Client-side error: %s" % r['faultstring']) + return r else: - r = dict(faultcode="Server", - faultstring=str(excinfo[1])) + faultstring = str(excinfo[1]) + debuginfo = "\n".join(traceback.format_exception(*excinfo)) + + log.error('Server-side error: "%s". Detail: \n%s' % ( + faultstring, debuginfo)) + + r = dict(faultcode="Server", faultstring=faultstring) if self._debug: - r['debuginfo'] = "Traceback:\n%s\n" % ( - "\n".join(traceback.format_exception(*excinfo))) + r['debuginfo'] = debuginfo return r def _lookup_function(self, path): diff --git a/wsme/rest.py b/wsme/rest.py index fd6fd06..5ac3bb0 100644 --- a/wsme/rest.py +++ b/wsme/rest.py @@ -41,7 +41,6 @@ class RestProtocol(object): res.status = "200 OK" except Exception, e: res.status = 500 - res.charset = 'utf8' res.body = self.encode_error( root._format_exception(sys.exc_info())) @@ -64,6 +63,6 @@ class RestProtocol(object): res_content_type = "text/html" res.body = html_body % dict(content=res.body) - res.headers['Content-Type'] = res_content_type + res.headers['Content-Type'] = "%s; charset=UTF-8" % res_content_type return res diff --git a/wsme/restjson.py b/wsme/restjson.py index 2430e66..a87443b 100644 --- a/wsme/restjson.py +++ b/wsme/restjson.py @@ -93,6 +93,8 @@ class RestJsonProtocol(RestProtocol): content_types = ['application/json', 'text/json', '', None] def decode_args(self, req, arguments): + if not req.body: + return {} raw_args = json.loads(req.body) kw = {} for farg in arguments: @@ -103,9 +105,10 @@ class RestJsonProtocol(RestProtocol): return kw def encode_result(self, result, return_type): - return json.dumps({'result': tojson(return_type, result)}) + r = tojson(return_type, result) + return json.dumps({'result': r}, ensure_ascii=False).encode('utf8') def encode_error(self, errordetail): - return json.dumps(errordetail) + return json.dumps(errordetail, encoding='utf-8') register_protocol(RestJsonProtocol)