format error message for duplicate device profile name created

Bug #1970818 reported by Wenping Song
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Cyborg (OpenStack)
New
Undecided
Unassigned

Bug Description

when create device profile with duplicate name, the error message is not format which is not standary and friendly, we cannot parse the exception message easily to the user.
the request url is: curl -g -i -X POST http://192.168.40.131/accelerator/v2/device_profiles -H "Content-Type: application/json" -H "OpenStack-API-Version: accelerator 2.0" -H "User-Agent: openstacksdk/0.58.0 keystoneauth1/4.3.1 python-requests/2.26.0 CPython/3.8.10" -H "X-Auth-Token: gAAAAABifa58MOAUyRiWh4Hqr8ramagSoLyzf8dkATWCWt2InZWiKvrh-dhbQhm7oYP5w9BrPYqRI2LRB_YaF-ZRQAUNLKnloYalG6rdngf30ctRFMaar_P_k1rrUrwCVG51_pVCpD0qVzBbpUBIr8Me-qO6uFsoDUVsauRiqNnDwqCL6D7NLV4" -d '[{"name": "aaa3","groups":[{"resources:FPGA":1, "trait:CUSTOM_FPGA_PRODUCT_ID_A115": "required", "trait:CUSTOM_FPGA_INSPUR": "required"}]}]'

{"error_message": "{\"faultcode\": \"Client\", \"faultstring\": \"A device_profile with name aaa3 already exists.\\nTraceback (most recent call last):\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py\\\", line 1771, in _execute_context\\n self.dialect.do_execute(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/default.py\\\", line 717, in do_execute\\n cursor.execute(statement, parameters)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/cursors.py\\\", line 148, in execute\\n result = self._query(query)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/cursors.py\\\", line 310, in _query\\n conn.query(q)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 548, in query\\n self._affected_rows = self._read_query_result(unbuffered=unbuffered)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 775, in _read_query_result\\n result.read()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 1156, in read\\n first_packet = self.connection._read_packet()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 725, in _read_packet\\n packet.raise_for_error()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/protocol.py\\\", line 221, in raise_for_error\\n err.raise_mysql_exception(self._data)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/err.py\\\", line 143, in raise_mysql_exception\\n raise errorclass(errno, errval)\\n\\npymysql.err.IntegrityError: (1062, \\\"Duplicate entry 'aaa3' for key 'device_profiles.uniq_device_profiles0name'\\\")\\n\\n\\nThe above exception was the direct cause of the following exception:\\n\\n\\nTraceback (most recent call last):\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/cyborg/db/sqlalchemy/api.py\\\", line 509, in device_profile_create\\n session.flush()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/session.py\\\", line 3298, in flush\\n self._flush(objects)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/session.py\\\", line 3438, in _flush\\n transaction.rollback(_capture_exception=True)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/langhelpers.py\\\", line 70, in __exit__\\n compat.raise_(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/compat.py\\\", line 207, in raise_\\n raise exception\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/session.py\\\", line 3398, in _flush\\n flush_context.execute()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/unitofwork.py\\\", line 456, in execute\\n rec.execute(self)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/unitofwork.py\\\", line 630, in execute\\n util.preloaded.orm_persistence.save_obj(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/persistence.py\\\", line 242, in save_obj\\n _emit_insert_statements(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/orm/persistence.py\\\", line 1219, in _emit_insert_statements\\n result = connection._execute_20(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py\\\", line 1583, in _execute_20\\n return meth(self, args_10style, kwargs_10style, execution_options)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/sql/elements.py\\\", line 323, in _execute_on_connection\\n return connection._execute_clauseelement(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py\\\", line 1452, in _execute_clauseelement\\n ret = self._execute_context(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py\\\", line 1814, in _execute_context\\n self._handle_dbapi_exception(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py\\\", line 1993, in _handle_dbapi_exception\\n util.raise_(newraise, with_traceback=exc_info[2], from_=e)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/util/compat.py\\\", line 207, in raise_\\n raise exception\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/base.py\\\", line 1771, in _execute_context\\n self.dialect.do_execute(\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/sqlalchemy/engine/default.py\\\", line 717, in do_execute\\n cursor.execute(statement, parameters)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/cursors.py\\\", line 148, in execute\\n result = self._query(query)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/cursors.py\\\", line 310, in _query\\n conn.query(q)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 548, in query\\n self._affected_rows = self._read_query_result(unbuffered=unbuffered)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 775, in _read_query_result\\n result.read()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 1156, in read\\n first_packet = self.connection._read_packet()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/connections.py\\\", line 725, in _read_packet\\n packet.raise_for_error()\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/protocol.py\\\", line 221, in raise_for_error\\n err.raise_mysql_exception(self._data)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/pymysql/err.py\\\", line 143, in raise_mysql_exception\\n raise errorclass(errno, errval)\\n\\noslo_db.exception.DBDuplicateEntry: (pymysql.err.IntegrityError) (1062, \\\"Duplicate entry 'aaa3' for key 'device_profiles.uniq_device_profiles0name'\\\")\\n[SQL: INSERT INTO device_profiles (created_at, updated_at, uuid, name, profile_json, description) VALUES (%(created_at)s, %(updated_at)s, %(uuid)s, %(name)s, %(profile_json)s, %(description)s)]\\n[parameters: {'created_at': datetime.datetime(2022, 5, 12, 9, 25, 59, 743919), 'updated_at': None, 'uuid': '233ee64a-dfd6-4697-8358-2f6f03efc468', 'name': 'aaa3', 'profile_json': '{\\\"groups\\\": [{\\\"resources:FPGA\\\": \\\"1\\\", \\\"trait:CUSTOM_FPGA_PRODUCT_ID_A115\\\": \\\"required\\\", \\\"trait:CUSTOM_FPGA_INSPUR\\\": \\\"required\\\"}]}', 'description': None}]\\n(Background on this error at: https://sqlalche.me/e/14/gkpj)\\n\\n\\nDuring handling of the above exception, another exception occurred:\\n\\n\\nTraceback (most recent call last):\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/oslo_messaging/rpc/server.py\\\", line 165, in _process_incoming\\n res = self.dispatcher.dispatch(message)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/oslo_messaging/rpc/dispatcher.py\\\", line 309, in dispatch\\n return self._do_dispatch(endpoint, method, ctxt, args)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/oslo_messaging/rpc/dispatcher.py\\\", line 229, in _do_dispatch\\n result = func(ctxt, **new_args)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/cyborg/conductor/manager.py\\\", line 57, in device_profile_create\\n obj_devprof.create(context)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/cyborg/objects/device_profile.py\\\", line 72, in create\\n db_devprof = self.dbapi.device_profile_create(context, values)\\n\\n File \\\"/usr/local/lib/python3.8/dist-packages/cyborg/db/sqlalchemy/api.py\\\", line 517, in device_profile_create\\n raise exception.DuplicateDeviceProfileName(\\n\\ncyborg.common.exception.DuplicateDeviceProfileName: A device_profile with name aaa3 already exists.\\n\", \"debuginfo\": null}"}

I would like the result is:
{'conflict':{'code':409,'message':'A device_profile with name aaa3 already exists.'}}
other exception such as badrequest, itemnotfound also need to format.

Wenping Song (wenping1)
description: updated
Wenping Song (wenping1)
description: updated
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.