需要根据我的Traceback提出异常

时间:2016-01-07 11:35:56

标签: python python-2.7 python-3.x openerp openerp-7

我正在努力在httplib类上生成请求和响应。 在这种情况下,我正在处理请求和响应字符串xml文件,以便我将一些请求发送到我的API服务器和服务器回复给我。 基于sting xml格式的请求和响应。

当我使用错误的网址格式发送数据时,它会给我一个错误,我需要根据Traceback下面的内容引发异常:

2016-01-07 11:27:40,443 12180 ERROR dynaweld_15_09_15 openerp.netsvc: mismatched tag: line 1, column 959
Traceback (most recent call last):
  File "/home/best/workspace/dynaweld/server/openerp/netsvc.py", line 292, in dispatch_rpc
    result = ExportService.getService(service_name).dispatch(method, params)
  File "/home/best/workspace/dynaweld/server/openerp/service/web_services.py", line 626, in dispatch
    res = fn(db, uid, *params)
  File "/home/best/workspace/dynaweld/server/openerp/osv/osv.py", line 188, in execute_kw
    return self.execute(db, uid, obj, method, *args, **kw or {})
  File "/home/best/workspace/dynaweld/server/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/best/workspace/dynaweld/server/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/best/workspace/dynaweld/server/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/best/workspace/dynaweld/prod_4/r3x_securepay/model/securepay_login.py", line 147, in test_secure_pay
    doc = minidom.parseString(data)
  File "/usr/lib/python2.7/xml/dom/minidom.py", line 1930, in parseString
    return expatbuilder.parseString(string)
  File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "/usr/lib/python2.7/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
ExpatError: mismatched tag: line 1, column 959

我曾尝试过以下方式来提高我的例外情况,但效果不佳。

from xml.parsers.expat import ExpatError
from openerp.osv import fields, osv, orm
from openerp.tools.translate import _
from openerp import netsvc
import httplib, ConfigParser, urlparse
import xmlrpclib
from xml.dom import minidom
from xml.parsers.expat import ExpatError
from datetime import datetime
import pytz

login_request="""<?xml version='1.0' encoding='UTF-8'?>
                    <SecurePayMessage>
                            <MessageInfo>
                                <messageID>%s_000000000000000</messageID>
                                <messageTimestamp>%s</messageTimestamp>
                                <timeoutValue>%d</timeoutValue>
                                <apiVersion>xml-4.2</apiVersion>
                            </MessageInfo>
                            <MerchantInfo>
                                <merchantID>%s</merchantID>
                                <password>%s</password>
                            </MerchantInfo>
                            <RequestType>Echo</RequestType>
                    </SecurePayMessage>"""%(timestamp_string[:14],timestamp_string,login_data.get('time_out',60),login_data.get('merchant_id',''),login_data.get('password',''))
try:
    urldat = urlparse.urlparse(login_data.get('url'))
    conn = httplib.HTTPSConnection(urldat[1])
    length  = len(login_request)
    GenerateHeaders = {"Content-Type":"text/xml","Content-Length":str(length)}
    conn.request("POST", urldat[2],login_request, GenerateHeaders)
    response = conn.getresponse()
    data = response.read()
except ExpatError:
    raise osv.except_osv(_('Error!'), _('Invalid Server URL from the API Configuration for SecurePay .!'))
except httplib.socket.error:    
    raise osv.except_osv(_('Error!'), _('Internet connection is not available !\t\n please try to test again using enabled Internet connection.'))
finally:
    conn.close()
return data

任何帮助都非常感激:)

0 个答案:

没有答案