From d47549344e353f4ba3e16effb7fd8e2ec4dca3bb Mon Sep 17 00:00:00 2001 From: Hongbin Lu Date: Fri, 8 Jun 2018 22:31:39 +0000 Subject: [PATCH] Add two fields to NeutronDbObjectDuplicateEntry When people were handling duplicated entry error from DB, sometimes they were trying to find out the specific columns where the value is duplicated. However, such information are not in the exception object which is inconvenient. People were even trying to do another query to find out the duplicated columns. This patch addresses it. Change-Id: If3ebcdfb5c3a5ddba1e2ebb6cb0ff79239e1ccd3 --- neutron_lib/objects/exceptions.py | 14 ++++++++++++++ ...uplicated-entry-exception-75b0e07c6e1cc6ae.yaml | 6 ++++++ 2 files changed, 20 insertions(+) create mode 100644 releasenotes/notes/add-two-fields-to-duplicated-entry-exception-75b0e07c6e1cc6ae.yaml diff --git a/neutron_lib/objects/exceptions.py b/neutron_lib/objects/exceptions.py index 32ec3d05a..1c935496c 100644 --- a/neutron_lib/objects/exceptions.py +++ b/neutron_lib/objects/exceptions.py @@ -21,6 +21,18 @@ class NeutronObjectUpdateForbidden(exceptions.NeutronException): class NeutronDbObjectDuplicateEntry(exceptions.Conflict): + """Duplicate entry at unique column error. + + Raised when made an attempt to write to a unique column the same entry as + existing one. :attr: `columns` available on an instance of the exception + and could be used at error handling:: + + try: + obj_ref.save() + except NeutronDbObjectDuplicateEntry as e: + if 'colname' in e.columns: + # Handle error. + """ message = _("Failed to create a duplicate %(object_type)s: " "for attribute(s) %(attributes)s with value(s) %(values)s") @@ -30,6 +42,8 @@ class NeutronDbObjectDuplicateEntry(exceptions.Conflict): fully_qualified=False), attributes=db_exception.columns, values=db_exception.value) + self.columns = db_exception.columns + self.value = db_exception.value class NeutronDbObjectNotFoundByModel(exceptions.NotFound): diff --git a/releasenotes/notes/add-two-fields-to-duplicated-entry-exception-75b0e07c6e1cc6ae.yaml b/releasenotes/notes/add-two-fields-to-duplicated-entry-exception-75b0e07c6e1cc6ae.yaml new file mode 100644 index 000000000..348d7f496 --- /dev/null +++ b/releasenotes/notes/add-two-fields-to-duplicated-entry-exception-75b0e07c6e1cc6ae.yaml @@ -0,0 +1,6 @@ +--- +other: + - | + Add two fields ``columns`` and ``value`` to exception + ``NeutronDbObjectDuplicateEntry``. These two fields are populated + from the corresponding db exception.