This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make it possible to enrich an exception's error message
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Zac Hatfield-Dodds, aroberge, gvanrossum, iritkatriel
Priority: normal Keywords: patch

Created on 2021-10-25 20:38 by iritkatriel, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29880 merged iritkatriel, 2021-12-01 15:33
Messages (4)
msg404999 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-10-25 20:38
The requirement comes from Hypothesis, see
https://github.com/python/cpython/pull/28569#discussion_r730338369

It is necessary there to add a note to an exception describing which test case it comes from. The note should be printed by __str__ of this exception. 


class Explanation(Exception):
    __module__ = "builtins"
    def __str__(self) -> str:
        return f"\n{self.args[0]}"

try:
    why = "Failed!"
    raise AssertionError(why)
except Exception as e:
    msg = "    You can reproduce this error by ...\n    ..."
    raise Explanation(msg) from e

    # Ideally something more like:
    e.__note__ = msg
    raise
msg405016 - (view) Author: Zac Hatfield-Dodds (Zac Hatfield-Dodds) * Date: 2021-10-26 00:48
This code shows my current best workaround based on a wrapper exception, with the traceback below annotating the additional details that I'd prefer to omit for clarity:


$ python example.py
Traceback (most recent call last):
  File "example.py", line 8, in <module>
    raise AssertionError(why)
AssertionError: Failed!
                                                                        # These lines are
The above exception was the direct cause of the following exception:    # confusing for new 
                                                                        # users, and they
Traceback (most recent call last):                                      # only exist due 
  File "example.py", line 10, in <module>                               # to implementation
    raise Explanation(msg) from e                                       # via the Explanation
Explanation:                                                            # wrapper type :-(
    You can reproduce this error by ...
    ...


The motivation for this is that we'd like to use ExceptionGroup to indicate that `MultipleFailures` is a group of exceptions, and replace our current print()-based method of reporting the details of the inner exceptions.
msg407365 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-30 11:54
Issue28953 is another use case for this feature.
msg407607 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-03 22:01
New changeset 5bb7ef2768be5979b306e4c7552862b1746c251d by Irit Katriel in branch 'main':
bpo-45607: Make it possible to enrich exception displays via setting their __note__ field (GH-29880)
https://github.com/python/cpython/commit/5bb7ef2768be5979b306e4c7552862b1746c251d
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89770
2021-12-03 22:02:13iritkatrielsetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.11
2021-12-03 22:01:23iritkatrielsetmessages: + msg407607
2021-12-01 15:33:08iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request28105
2021-11-30 11:54:31iritkatrielsetmessages: + msg407365
2021-10-26 00:48:38Zac Hatfield-Doddssetmessages: + msg405016
2021-10-26 00:35:58arobergesetnosy: + aroberge
2021-10-25 23:18:56gvanrossumsetnosy: + Zac Hatfield-Dodds
2021-10-25 23:18:12gvanrossumsetnosy: + gvanrossum
2021-10-25 20:38:01iritkatrielcreate