#33331: Improve exception handling with `cursor.close()` after errors
-------------------------------------+-------------------------------------
     Reporter:  Daniel Hahler        |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Database layer       |                  Version:  3.2
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:  needsinfo
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

 * status:  new => closed
 * resolution:   => needsinfo


Comment:

 Hi Daniel.

 I'm really sorry but I'm failing to see the point you're trying to make
 here... There's clearly something I'm missing because you keep trying to
 get it across to me, but I just don't get it. Can I **please** ask you to
 take a step back and explain it to me from the top clearly so that I can
 see the issue?

 It looks to me like the code we have here when there's no exception in
 `cursor.close()` is equivalent to this:


 {{{
 def will_raise(msg):
     raise Exception(msg)


 try:
     will_raise("Outer")
 except Exception:
     raise
 }}}

 When I run that, I get this output:


 {{{
 % python3 exceptions.py
 Traceback (most recent call last):
   File "/Users/carlton/Desktop/exceptions.py", line 6, in <module>
     will_raise("Outer")
   File "/Users/carlton/Desktop/exceptions.py", line 2, in will_raise
     raise Exception(msg)
 Exception: Outer
 }}}

 When there **is** an exception in `cursor.close()` it looks like this:

 {{{
 def will_raise(msg):
     raise Exception(msg)


 try:
     will_raise("Outer")
 except Exception:
     will_raise("Inner")
     raise
 }}}


 Which gives this output:


 {{{
 % python3 exceptions.py
 Traceback (most recent call last):
   File "/Users/carlton/Desktop/exceptions.py", line 6, in <module>
     will_raise("Outer")
   File "/Users/carlton/Desktop/exceptions.py", line 2, in will_raise
     raise Exception(msg)
 Exception: Outer

 During handling of the above exception, another exception occurred:

 Traceback (most recent call last):
   File "/Users/carlton/Desktop/exceptions.py", line 8, in <module>
     will_raise("Inner")
   File "/Users/carlton/Desktop/exceptions.py", line 2, in will_raise
     raise Exception(msg)
 Exception: Inner
 }}}


 Your suggestion seems to be to make it equivalent to:


 {{{
 def will_raise(msg):
     raise Exception(msg)


 try:
     will_raise("Outer")
 except Exception as o:
     try:
         will_raise("Inner")
     except Exception as i:
         raise i from o
     raise
 }}}

 ... which has the output:


 {{{
 % python3 exceptions.py
 Traceback (most recent call last):
   File "/Users/carlton/Desktop/exceptions.py", line 19, in <module>
     will_raise("Outer")
   File "/Users/carlton/Desktop/exceptions.py", line 15, in will_raise
     raise Exception(msg)
 Exception: Outer

 The above exception was the direct cause of the following exception:

 Traceback (most recent call last):
   File "/Users/carlton/Desktop/exceptions.py", line 24, in <module>
     raise i from o
   File "/Users/carlton/Desktop/exceptions.py", line 22, in <module>
     will_raise("Inner")
   File "/Users/carlton/Desktop/exceptions.py", line 15, in will_raise
     raise Exception(msg)
 Exception: Inner
 }}}

 Which is exactly the same bar the "direct cause" wording change, that was
 agreed on the mailing list discussion to be not worth the verbosity of the
 code change.

 So what's it fixing? (Again: Sorry to not be seeing this, but I'm just not
 getting the point you want to make.)

 > It is meant to fix
 https://code.djangoproject.com/ticket/16614#comment:31 again, which
 regressed.

 But it didn't regress. Both `Outer` and `Inner` are given in the
 traceback. `Outer` is given first, plus the additional info that `Inner`
 occurred whilst we were handling that.
 This behaviour in Python 3 is what justifies the change in
 
https://github.com/django/django/commit/d170c63351944fd91b2206d10f89e7ff75b53b76
 #diff-
 f58de2deaccecd2d53199c5ca29e3e1050ec2adb80fb057cdfc0b4e6accdf14fR878-R879
 — the original error **is** raised.

 If you can put together an actual example of the problem you're hitting
 coming up, that might make it easier. It's very hard to see when it's just
 a couple of lines changed with no test-case or reproduce. (I appreciate
 cursor errors can be hard to test...)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33331#comment:7>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.36ca61ecfc5b0942621f863987051636%40djangoproject.com.

Reply via email to