First of all, here are similar questions that address this problem:
1. Raising exceptions without 'raise' in the traceback?
2. Don't show Python raise-line in the exception stack
No, it is not a duplicate, as the answers don't actually solve the problem.
I don't want to format the actual traceback
object. Instead, I want to just format the way it is displayed to the user.
The Code
source.py
class HelloError(Exception):
pass
def type_hello_here(string):
if string != 'hello':
raise HelloError("You must type 'hello', not '{}'".format(string))
script.py
from source import type_hello_here
type_hello_here('hello') # No error here
type_hello_here('booty') # Obviously returns an error because booty != hello
The Bad Exception
Traceback (most recent call last):
File "D:/python/script.py", line 3, in <module>
type_hello_here('booty')
File "D:\python\source.py", line 6, in type_hello_here
raise HelloError("You must type 'hello', not '{}'".format(string))
source.HelloError: You must type 'hello', not 'booty'
The Desired Exception
Traceback (most recent call last):
File "D:/python/script.py", line 3, in <module>
type_hello_here('booty')
HelloError: You must type 'hello', not 'booty'
I want to format it in such a way that the last entry, pointing to the code in the source module is not displayed, as the error does not actually occur there. Also, I want the source in 'source.HelloError' to disappear.