0
我不明白为什么我的程序没有返回带有我在try catch块中定义的错误代码的json响应。返回处理异常
json = request.json
return jsonify(controller.new_configuration(json))
这是Controller.py
def new_configuration(self, data):
try:
self.config.read('system/settings.ini')
self.config.set('service_configuration', 'servid', data['servID'])
self.config.set('service_configuration', 'uri', data['uri'])
self.config.set('service_configuration', 'res_temp_min', data['res_temp_min'])
for param in data['modelParameters']:
self.config.set('model_param', param['k'], param['v'])
with open('system/settings.ini', 'w') as configfile:
self.config.write(configfile)
except TypeError as e:
return {"rc": 1, "rcDesc": e}
except ValueError as e:
return {"rc": 1, "rcDesc": e}
except KeyError as e:
self.log.warning(e)
return {"rc": 1, "rcDesc": e}
except AttributeError as e:
self.log.warning(e)
return {"rc": 1, "rcDesc": e}
return {"rc": 0, "rcDesc": "ok"}
林我的功能new_configuration故意发送一个TypeError JSON,我想它应该返回{ “RC”:1, “rcDesc用”:e},但不是。 我得到这个错误,而不是与应用程式的空档:
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: KeyError('servID',) is not JSON serializable
你收到的错误消息中有哪些不清楚?你会期望一个Exception实例被序列化为什么? –
你应该用'try/except'块来包围'jsonify' ......它看起来像这个函数抛出异常。 –