2

This is a semantics question, but I'm trying to determine the correct Error to throw in the following situation.

I have a process that sometimes is performed multiple times. If the process is performed an additional time before the first time is complete, we have thread protection that raises a DatabaseError and blocks the process from occurring a second time. Instead of returning the generic DatabaseError, this raises a custom error that says that the original process is still incomplete.

In lieu of the custom error, is there a "by the books" PEP error that would be more appropriate? I've read through the python PEP for database exceptions and none stuck out to me.

https://peps.python.org/pep-0249/#exceptions

pygeek
  • 7,356
  • 1
  • 20
  • 41
Asa LeHolland
  • 372
  • 4
  • 12
  • 1
    This PEP is for Python internal stuff relating to database access. In your own project, it's always a good idea to create custom exceptions for errors that are specific to your application. Also, it seems your question is related to threading, i.e. what error to raise when a thread would block? – a_guest Jul 26 '22 at 18:43
  • 1
    Side note: From your description it sounds that `DatabaseError` is completely misfitting. You are not reporting problems related to DB really (like queries). – Marcin Orlowski Jul 26 '22 at 18:44
  • 1
    This doesn't really seem DB related. It's possible one of [the built-in exceptions](https://docs.python.org/3/library/exceptions.html#os-exceptions) like `BlockingIOError` (raised when an operation would block, but you explicitly said not to block) or `ChildProcessError` (general "child process issue") would make sense. Or a [subclass of `subprocess.SubprocessError`](https://docs.python.org/3/library/subprocess.html#subprocess.SubprocessError). It's not really clear what you're trying to convey, but it definitely doesn't seem like a DB-related error to integrate into that hierarchy. – ShadowRanger Jul 26 '22 at 19:12

1 Answers1

2

Problem

There is no PEP Error specification for what you would like to accomplish, however, there is an idiosyncrasy that will achieve the similar—and better—effect, which allows one to retain error context while being able to add additional information.

threading.excepthook

Within the thread, continue to use a relevant error. Outside of the thread, just use a threading.excepthook, where you can affix your error with additional context (ie: "Thread x: raised SomeException").

Example:

def custom_hook(args):
    print(f'Thread {args.thread}: raised {args.exc_type} {args.exc_value}: {args.exc_traceback}')
 
threading.excepthook = custom_hook
thread = threading.Thread(target=foo)

thread.start()
thread.join()

...

Notes

It is encouraged to create your own custom Exceptions where necessary. What's important is that the Error effectively expresses the context of what is happening to allow for debugging more efficiently.

References

https://docs.python.org/3/library/threading.html#threading.excepthook

https://docs.python.org/3/library/sys.html#sys.excepthook

pygeek
  • 7,356
  • 1
  • 20
  • 41