this is how I translate the thrown errors:
catch (NpgsqlException ex)
{
if (tx != null) tx.Rollback();
if (ex.Message.Contains("foreign key constraint"))
throw new Exception("Cannot delete this, this record is already referenced on other record(s)");
else
throw new Exception(ex.Message, ex.InnerException);
}
Npgsql sample constraint error:
ERROR: 23503: update or delete on table
“color” violates foreign key
constraint “fk_order_detail__color” on
table “order_detail”
You should write your problem that way so it doesn’t cause SQL errors.
Or, if you know specific cause of a error happening, just show it in interface as a message, not a error. I.e. “you should delete dependencies between deleting this object” not “internal error: whatever”.
Error is something unexpected; your constraint errors aren’t.