PrettyErrors icon indicating copy to clipboard operation
PrettyErrors copied to clipboard

Only last Exception shown

Open pmatias opened this issue 6 years ago • 13 comments

While dealing with multiple exceptions ("During handling of the above exception, another exception occurred: ..."), Pretty Errors will show only the last one.

Is there a way to show all of them?

pmatias avatar Mar 16 '20 14:03 pmatias

This could be a bug in pretty errors; this is the message that will display if pretty errors raises an exception while handling an exception. Or it could be something else. Can you post a screenshot of it happening? What does it look like when the same error happens and you are not using pretty errors?

onelivesleft avatar Mar 16 '20 15:03 onelivesleft

Sure!

With pretty-errors (I'm removing sensitive info from the traceback)

*******************************************************************************
11 <module> /usr/bin/***
> load_entry_point('***==1.2.2', 'console_scripts', '***')()
---
70 main /usr/lib/python3.7/site-packages/***/cmdline.py
> scenario.execute()
---
154 execute /mnt/***/********.py
|     self.deploy_site_repo()
| except Exception:
|     if self.payload.get("individual_notifications", True):
|         self.build_data["task"]["status"] = "halt"
|         self.sns.publish_message(self.build_data)
>     raise EnvironmentError
| 
| self.shutdown()
= self: <**********.DeploySiteRepo object at 0x7f18791efb90>
OSError

Without pretty-errors

Traceback (most recent call last):
  File " /mnt/***/********.py", line 149, in execute
    self.deploy_site_repo()
  File "/mnt/ /mnt/***/********.py.py", line 81, in deploy_site_repo
    self.config.get("****", "url"))
  File "/usr/lib/python3.7/site-packages/***/services/composer.py", line 162, in run
    cmd = self._get_composer_cmd(action, *args)
  File "/usr/lib/python3.7/site-packages/***/services/composer.py", line 183, in _get_composer_cmd
    arguments = ' '.join(args) if args else ""
TypeError: sequence item 5: expected str instance, NoneType found
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/bin/***", line 11, in <module>
    load_entry_point('***==1.2.2', 'console_scripts', '***')()
  File "/usr/lib/python3.7/site-packages/***/cmdline.py", line 70, in main
    scenario.execute()
  File " /mnt/***/********.py", line 154, in execute
    raise EnvironmentError
OSError

pmatias avatar Mar 16 '20 15:03 pmatias

Is there any way you could make a small script which reproduces this scenario, which I could use to work against?

It's not currently built to take this situation into account, but in the meantime you could try pretty_errors.replace_stderr(); it should give you output containing both traces, though I'm not sure how well it will be formatted.

onelivesleft avatar Mar 16 '20 15:03 onelivesleft

Thanks, I'll try your workaround, in the meantime, this should generate a double exception:

try:
    myval = [1,2]
    print(myval[3])
except:
    print("First exception")
    raise OSError

pmatias avatar Mar 16 '20 15:03 pmatias

This should now be handled correctly if you update to v1.2.16. See config options inner_exception_message and inner_exception_separator in the readme if you want to config it.

onelivesleft avatar Mar 16 '20 17:03 onelivesleft

This is great @onelivesleft, thanks for the amazingly quick response on this!

I'll be trying it out in a few minutes.

pmatias avatar Mar 17 '20 11:03 pmatias

I've just tried it in my project, and indeed works as expected!

Thanks again for taking care of this!

pmatias avatar Mar 17 '20 12:03 pmatias

👍

onelivesleft avatar Mar 17 '20 12:03 onelivesleft

This also seems to break when you explicitly don't want to show the inner exception - which you can control in python normally by rethrowing an error like this:

def my_function():
   raise ValueError("big problems!")

try:
   my_function():
catch ValueError as e:
  raise e from None

The stack trace should hide the inner exception (i.e. there should be no mention of my_function in the stack trace)

oliver-batchelor avatar Apr 15 '20 15:04 oliver-batchelor

I'm not following you, normal python shows it too:

▶ python regular.py
Traceback (most recent call last):
  File "regular.py", line 8, in <module>
    raise e from None
  File "regular.py", line 6, in <module>
    my_function()
  File "regular.py", line 3, in my_function
    raise ValueError("big problems!")
ValueError: big problems!

▶ python pretty.py

pretty.py 9 <module>
raise e from None

pretty.py 7 <module>
my_function()

pretty.py 4 my_function
raise ValueError("big problems!")
ValueError
:
big problems!

onelivesleft avatar Apr 15 '20 23:04 onelivesleft

Hi ! Sorry my example was not a good one, I realise that to have the desired behaviour it is necessary to throw a new exception and not just re-throw the old one.

To see the difference in normal python remove the "from None" which then matches the output from pretty_errors.

def my_function():
   raise ValueError("big problems!")

try:
   my_function()
except ValueError as e:
  raise ValueError("test") from None

python regular.py

Traceback (most recent call last):
  File "regular.py", line 9, in <module>
    raise ValueError("test") from None
ValueError: test

with pretty_errors:

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
regular.py 7 <module>
my_function()

regular.py 4 my_function
raise ValueError("big problems!")

ValueError:
big problems!

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
regular.py 9 <module>
raise ValueError("test") from None

ValueError:
test

It just happened that I was learning about this feature at the same time I had installed pretty_errors (which looks great by the way!) and was very confused why I didn't see any difference.

oliver-batchelor avatar Apr 16 '20 08:04 oliver-batchelor

The behaviour is described in https://docs.python.org/3/library/exceptions.html - it sets a flag __cause__ (and __suppress_context__) on the exception which makes it hide the inner exception.

oliver-batchelor avatar Apr 16 '20 08:04 oliver-batchelor

Thanks for the link, updated to v1.2.17.

onelivesleft avatar Apr 18 '20 11:04 onelivesleft