最近用webpy开发web应用,不出所料刚开始果然遇到很多未知问题,这里总结一下。
调试模式下异常捕捉问题,在开发过程中我在操作数据库是老是遇到以下的这个问题(实际上所有异常都报这个错误...)
File "D:\����\eclipesWork\python\PyTest\src\web\debugerror.py", line 305, in debugerror
return web._InternalError(djangoerror())
File "D:\����\eclipesWork\python\PyTest\src\web\debugerror.py", line 295, in djangoerror
return t(exception_type, exception_value, frames)
File "D:\����\eclipesWork\python\PyTest\src\web\template.py", line 881, in __call__
return BaseTemplate.__call__(self, *a, **kw)
File "D:\����\eclipesWork\python\PyTest\src\web\template.py", line 808, in __call__
return self.t(*a, **kw)
File "D:\����\eclipesWork\python\PyTest\src\web\debugerror.pyc", line 153, in __template__
<ul class="traceback">
File "D:\����\eclipesWork\python\PyTest\src\web\template.py", line 825, in _escape
value = safeunicode(value)
File "D:\����\eclipesWork\python\PyTest\src\web\utils.py", line 348, in safeunicode
return obj.decode(encoding)
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xb9 in position 3: invalid start byte
这样的问题让我很是纠结因为我根本看不到具体是什么问题。解决方式建一个超类捕获GET与POST的异常.
# -*- coding: utf-8 -*-
'''
Created on 2013-11-19
@author: 梁前武 QQ:1587790525
'''
class baseweb():
def myGET(self):
return "baseweb get"
def myPOST(self):
return "baseweb post"
def GET(self):
v=""
try:
v=self.myGET()
except Exception,e:
return "erro %s"%(e)
return v
def POST(self):
v=""
try:
v=self.myPOST()
except Exception,e:
return "erro %s"%(e)
return v
以后所有的操作类都继承这个超类并且实现对应的myGET和myPOST方法(不用GET和POST方法)
class userreg(baseweb):
def myGET(self):
...
这样出现异常以后可以直接在调试网页显示异常信息如下图
![](http://static.oschina.net/uploads/space/2013/1119/152447_gkeY_138488.png)