Skip to content

Instantly share code, notes, and snippets.

@zzzeek
Last active March 2, 2020 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzzeek/77a2b79d58d547411a518fd5003fc78c to your computer and use it in GitHub Desktop.
Save zzzeek/77a2b79d58d547411a518fd5003fc78c to your computer and use it in GitHub Desktop.
pep 409 / 415 don't really solve the problem for libraries
"""pep-409 and pep-415 add support for "raise exception from None" so that
libraries need not expose internal exception cases.
However, this is too broad in scope as it breaks user-land code that would
like to still see the context of where *their* application was handling
the error.
This makes pep-409/415 more or less useless and the problem they'd like
to solve still remains unsolved.
"""
class MyLibrary:
def get_thing(self):
try:
raise KeyError(
"didn't find some internal thing, nobody should "
"see this in their logs")
except KeyError as err:
raise Exception(
"Can't do the mylibrary get_thing, "
"this is the real error, so I raise from None "
"here, but I realy just want 'err' skipped, not external "
"context") from None
# here's what I want - targeted ignore for just the *immediate*
# exception. I've tried to hack this together w/o success:
raise Exception(
"Can't do the mylibrary get_thing"
).replaces(err)
def my_userland_application():
library = MyLibrary()
try:
raise IndexError(
"some end-user application caught exception that i "
"really want to see in my logs, but we won't because "
"MyLibrary is suppressing all exception context")
except:
library.get_thing()
# does not show my IndexError
my_userland_application()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment