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.