How to shorten the error text for a request response?

51 views Asked by At

in odoo15 framework I have a controller for api. in some cases I need to return response with status 500 and short error log.

when i try to: raise Exception('test message') odoo returns log response like this:

{"jsonrpc": "2.0", "id": null, "error": {"code": 200, "message": "Odoo Server Error", "data": {"name": "odoo.exceptions.AccessError", "debug": "Traceback (most recent call last):\n  File \"/usr/lib/python3/dist-packages/odoo/addons/base/models/ir_http.py\", line 237, in _dispatch\n    result = request.dispatch()\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 687, in dispatch\n    result = self._call_function(**self.params)\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 359, in _call_function\n    return checked_call(self.db, *args, **kwargs)\n  File \"/usr/lib/python3/dist-packages/odoo/service/model.py\", line 94, in wrapper\n    return f(dbname, *args, **kwargs)\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 348, in checked_call\n    result = self.endpoint(*a, **kw)\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 916, in __call__\n    return self.method(*args, **kw)\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 535, in response_wrap\n    response = f(*args, **kw)\n  File \"/var/odoo/custom/tada/controllers/schedule.py\", line 159, in update_department_budget\n    raise AccessError('test')\nException\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 643, in _handle_exception\n    return super(JsonRequest, self)._handle_exception(exception)\n  File \"/usr/lib/python3/dist-packages/odoo/http.py\", line 301, in _handle_exception\n    raise exception.with_traceback(None) from new_cause\nodoo.exceptions.AccessError: test\n", "message": "test message", "arguments": ["test message"], "context": {}}}}

which is very long, contains file names, and other developers need to search my message in this mess.

i tried another way to create a request.Response() object:

return request.Response('test message', status_code=500)

but it returns result without my message:

'{"jsonrpc": "2.0", "id": null, "result": "<Response 4 bytes [500 INTERNAL SERVER ERROR]>"}'

is there a ways to do this?

1

There are 1 answers

0
Yassir Irfan On
res = {'status': 'error', 'error': 'Invalid API Key'}
return request.Response(response=json.dumps(res), status=500)

Try this which creates a response object with the JSON-formatted string as the response content and a status code of 500 (Internal Server Error).