Fix some issues with encoding
This commit is contained in:
parent
585c6d703f
commit
cbfd17491f
@ -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)
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user