How do i hide the internal Error Stack [python] - python

In the following code, i want to prevent the internal error traceback to be shown.
(In lua, the equivalent would be error("info",2)).
Is this also possible in Python?
def asd():
def efe(number = 2):
try:
number += 2
print(number)
except TypeError as e:
failed = True
# raise TypeError() # makes it even worse
if failed:
raise TypeError() # dont show internal stuff please
# raise TypeError().with_traceback() # i guess use this is the way
efe(number = "asd") # i want the end of Error Stack here
asd()
prints
Traceback (most recent call last):
File "main.py", line 15, in <module>
asd()
File "main.py", line 13, in asd
efe(number = "asd") # i want the end of Error Stack here
File "main.py", line 10, in efe
raise TypeError() # dont show internal stuff please
TypeError

I don't think this is possible in python. You could just print out an "error" as plain text, though.
EDIT: Try using the traceback module to format your error messages.

Related

Is it possible to edit the error template in Maya using python?

I'm currently learning how to code in python and I was trying to raise exceptions in my code to make my tool more user-friendly. However, I find the template given by Maya to be too simple and I'd like to have more control over it.
Normally, the base template looks a little like this:
raise Exception(errorTitle)
>>> Result
# Error: errorTitle
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# Exception: errorTitle #
It works fine, but it doesn't let you give a more detailed description of the error. What I'm trying to achieve would look a little more like this:
raise Exception(errorTitle, errorDescription)
>>> Result
# Error: errorTitle
# errorDescription
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# Exception: errorTitle #
I also tried putting the errorDescription into errorTitle by adding a new line to the string, but since errorTitle is repeated twice, it becomes confusing quickly when trying to sort errors.
I know you can't really use multiple arguments in the raise function, so I was wondering if there was an error template in Maya I could reference instead of raising an error.
Alternatively, is there a way to create my own error template? If I can reuse the Traceback function and change the script bar color, it might be easier to just do that.
Thanks in advance!
You can get your desired result just like that:
>>> raise Exception("Error Title\nError description that explains the problem")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: Error Title
Error description that explains the problem
Or you may want to declare your custom exception class:
class YourException(Exception):
def __init__(self, title, description):
super(YourException, self).__init__(title + "\n" + description)
and then you can use your original syntax:
>>> raise YourException("Error Title", "Error description that explains the problem")
File "<stdin>", line 1, in <module>
__main__.YourException: Error Title
Error description that explains the problem

Modifying exception message without losing stack from raise

I've got a try/except clause that will either return or catch a KeyError as follows:
try:
return super().__new__(globals()[kls])
except KeyError:
raise
This will generate a stack trace when used improperly like so:
>>> g = Grid(cell='Circle')
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
g = Grid(cell='Circle')
File "<pyshell#1>", line 8, in __new__
return super().__new__(globals()[kls])
KeyError: 'SHPCircleGrid'
>>> g
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
g
NameError: name 'g' is not defined
And that is perfectly fine, however I want to "extend/modify" the message to explain to the user how to avoid this error again; i.e. go from:
KeyError: 'SHPCircleGrid'
to
KeyError: 'SHPCircleGrid'. Use 'Hex', 'Rect' or 'Tri' for cell keyword.
While maintaining the stack for the user. A generic print() in the catching portion sets g to NoneType which I would not like, so simply printing is not the way to handle this. Adding another raise KeyError('some message') prints two stacks (the "while handling exception another..." message) which is also undesired.
What is the appropriate way to handle this such that it could be extended to any additional keywords that the class instantiation may throw a KeyError for?
Can't you just achieve that by supplying your message to KeyError as such:
try:
return super().__new__(globals()[kls])
except KeyError as e:
key = "'{}'".format(*e.args)
base = "{}. Use 'Hex', 'Rect' or 'Tri' for cell keyword."
raise KeyError(base.format(key)) from None

traceback.print_exc() shows incomplete stack trace

The traceback module is great for capturing and handling exceptions, but in the following example it seems to capture an incomplete stack from the most recent exception.
Consider two files, one say "mymod.py":
import sys, traceback
def func():
try:
otherfunc()
except:
raise
#traceback.print_exc()
def otherfunc():
raise Exception( 'fake' )
And another one say "myfile.py":
from mymod import *
func()
Running myfile.py gives:
Traceback (most recent call last):
File "myfile.py", line 2, in <module>
func()
File "/Users/rrdrake/temp/mymod.py", line 5, in func
otherfunc()
File "/Users/rrdrake/temp/mymod.py", line 11, in otherfunc
raise Exception( 'fake' )
Now change mymod.py to comment out the "raise" and uncomment the print_exc line. Then we get
Traceback (most recent call last):
File "/Users/rrdrake/temp/mymod.py", line 5, in func
otherfunc()
File "/Users/rrdrake/temp/mymod.py", line 11, in otherfunc
raise Exception( 'fake' )
Exception: fake
Notice that print_exc() does not include the myfile.py frame while re-raising does. How can I make print_exc() include the origin calling frame?
Note that if I add a traceback.print_stack() in the except block, it does include the myfile.py frame, so the information seems to be available.
In your first case, the exception from your raise call bubbles up at the top level of the script. Python calls sys.excepthook(), which displays the full traceback.
In your second case, the exception is caught and print_exc() is used to print the exception, and this stops at the calling function (otherfunc() in your case) so you don't get the full traceback.
You can change that by using your own traceback/exception print function,
something along those lines:
def print_full_stack(tb=None):
if tb is None:
tb = sys.exc_info()[2]
print 'Traceback (most recent call last):'
for item in reversed(inspect.getouterframes(tb.tb_frame)[1:]):
print ' File "{1}", line {2}, in {3}\n'.format(*item),
for line in item[4]:
print ' ' + line.lstrip(),
for item in inspect.getinnerframes(tb):
print ' File "{1}", line {2}, in {3}\n'.format(*item),
for line in item[4]:
print ' ' + line.lstrip(),
Just replace traceback.print_exc() with print_full_stack(). This function involves the inspect module to get code frames.
You can read this blog for much more information: http://blog.dscpl.com.au/2015/03/generating-full-stack-traces-for.html
Beware, the code in the above article has some indentation errors...

Raise an exception from a higher level, a la warnings

In the module warnings (https://docs.python.org/3.5/library/warnings.html) there is the ability to raise a warning that appears to come from somewhere earlier in the stack:
warnings.warn('This is a test', stacklevel=2)
Is there an equivalent for raising errors? I know I can raise an error with an alternative traceback, but I can't create that traceback within the module since it needs to come from earlier. I imagine something like:
tb = magic_create_traceback_right_here()
raise ValueError('This is a test').with_traceback(tb.tb_next)
The reason is that I am developing a module that has a function module.check_raise that I want to raise an error that appears to originate from where the function is called. If I raise an error within the module.check_raise function, it appears to originate from within module.check_raise, which is undesired.
Also, I've tried tricks like raising a dummy exception, catching it, and passing the traceback along, but somehow the tb_next becomes None. I'm out of ideas.
Edit:
I would like the output of this minimal example (called tb2.py):
import check_raise
check_raise.raise_if_string_is_true('True')
to be only this:
Traceback (most recent call last):
File "tb2.py", line 10, in <module>
check_raise.raise_if_string_is_true(string)
RuntimeError: An exception was raised.
I can't believe I am posting this
By doing this you are going against the zen.
Special cases aren't special enough to break the rules.
But if you insist here is your magical code.
check_raise.py
import sys
import traceback
def raise_if_string_is_true(string):
if string == 'true':
#the frame that called this one
f = sys._getframe().f_back
#the most USELESS error message ever
e = RuntimeError("An exception was raised.")
#the first line of an error message
print('Traceback (most recent call last):',file=sys.stderr)
#the stack information, from f and above
traceback.print_stack(f)
#the last line of the error
print(*traceback.format_exception_only(type(e),e),
file=sys.stderr, sep="",end="")
#exit the program
#if something catches this you will cause so much confusion
raise SystemExit(1)
# SystemExit is the only exception that doesn't trigger an error message by default.
This is pure python, does not interfere with sys.excepthook and even in a try block it is not caught with except Exception: although it is caught with except:
test.py
import check_raise
check_raise.raise_if_string_is_true("true")
print("this should never be printed")
will give you the (horribly uninformative and extremely forged) traceback message you desire.
Tadhgs-MacBook-Pro:Documents Tadhg$ python3 test.py
Traceback (most recent call last):
File "test.py", line 3, in <module>
check_raise.raise_if_string_is_true("true")
RuntimeError: An exception was raised.
Tadhgs-MacBook-Pro:Documents Tadhg$
If I understand correctly, you would like the output of this minimal example:
def check_raise(function):
try:
return function()
except Exception:
raise RuntimeError('An exception was raised.')
def function():
1/0
check_raise(function)
to be only this:
Traceback (most recent call last):
File "tb2.py", line 10, in <module>
check_raise(function)
RuntimeError: An exception was raised.
In fact, it's a lot more output; there is exception chaining, which could be dealt with by handling the RuntimeError immediately, removing its __context__, and re-raising it, and there is another line of traceback for the RuntimeError itself:
File "tb2.py", line 5, in check_raise
raise RuntimeError('An exception was raised.')
As far as I can tell, it is not possible for pure Python code to substitute the traceback of an exception after it was raised; the interpreter has control of adding to it but it only exposes the current traceback whenever the exception is handled. There is no API (not even when using tracing functions) for passing your own traceback to the interpreter, and traceback objects are immutable (this is what's tackled by that Jinja hack involving C-level stuff).
So further assuming that you're interested in the shortened traceback not for further programmatic use but only for user-friendly output, your best bet will be an excepthook that controls how the traceback is printed to the console. For determining where to stop printing, a special local variable could be used (this is a bit more robust than limiting the traceback to its length minus 1 or such). This example requires Python 3.5 (for traceback.walk_tb):
import sys
import traceback
def check_raise(function):
__exclude_from_traceback_from_here__ = True
try:
return function()
except Exception:
raise RuntimeError('An exception was raised.')
def print_traceback(exc_type, exc_value, tb):
for i, (frame, lineno) in enumerate(traceback.walk_tb(tb)):
if '__exclude_from_traceback_from_here__' in frame.f_code.co_varnames:
limit = i
break
else:
limit = None
traceback.print_exception(
exc_type, exc_value, tb, limit=limit, chain=False)
sys.excepthook = print_traceback
def function():
1/0
check_raise(function)
This is the output now:
Traceback (most recent call last):
File "tb2.py", line 26, in <module>
check_raise(function)
RuntimeError: An exception was raised.
EDIT: The previous version did not provide quotes or explanations.
I suggest referring to PEP 3134 which states in the Motivation:
Sometimes it can be useful for an exception handler to intentionally
re-raise an exception, either to provide extra information or to
translate an exception to another type. The __cause__ attribute
provides an explicit way to record the direct cause of an exception.
When an Exception is raised with a __cause__ attribute the traceback message takes the form of:
Traceback (most recent call last):
<CAUSE TRACEBACK>
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
<MAIN TRACEBACK>
To my understanding this is exactly what you are trying to accomplish; clearly indicate that the reason for the error is not your module but somewhere else. If you are instead trying to omit information to the traceback like your edit suggests then the rest of this answer won't do you any good.
Just a note on syntax:
The __cause__ attribute on exception objects is always initialized
to None. It is set by a new form of the 'raise' statement:
raise EXCEPTION from CAUSE
which is equivalent to:
exc = EXCEPTION
exc.__cause__ = CAUSE
raise exc
so the bare minimum example would be something like this:
def function():
int("fail")
def check_raise(function):
try:
function()
except Exception as original_error:
err = RuntimeError("An exception was raised.")
raise err from original_error
check_raise(function)
which gives an error message like this:
Traceback (most recent call last):
File "/PATH/test.py", line 7, in check_raise
function()
File "/PATH/test.py", line 3, in function
int("fail")
ValueError: invalid literal for int() with base 10: 'fail'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/PATH/test.py", line 12, in <module>
check_raise(function)
File "/PATH/test.py", line 10, in check_raise
raise err from original_error
RuntimeError: An exception was raised.
However the first line of the cause is the statement in the try block of check_raise:
File "/PATH/test.py", line 7, in check_raise
function()
so before raising err it may (or may not) be desirable to remove the outer most traceback frame from original_error:
except Exception as original_error:
err = RuntimeError("An exception was raised.")
original_error.__traceback__ = original_error.__traceback__.tb_next
raise err from original_error
This way the only line in the traceback that appears to come from check_raise is the very last raise statement which cannot be omitted with pure python code although depending on how informative the message is you can make it very clear that your module was not the cause of the problem:
err = RuntimeError("""{0.__qualname__} encountered an error during call to {1.__module__}.{1.__name__}
the traceback for the error is shown above.""".format(function,check_raise))
The advantage to raising exception like this is that the original Traceback message is not lost when the new error is raised, which means that a very complex series of exceptions can be raised and python will still display all the relevant information correctly:
def check_raise(function):
try:
function()
except Exception as original_error:
err = RuntimeError("""{0.__qualname__} encountered an error during call to {1.__module__}.{1.__name__}
the traceback for the error is shown above.""".format(function,check_raise))
original_error.__traceback__ = original_error.__traceback__.tb_next
raise err from original_error
def test_chain():
check_raise(test)
def test():
raise ValueError
check_raise(test_chain)
gives me the following error message:
Traceback (most recent call last):
File "/Users/Tadhg/Documents/test.py", line 16, in test
raise ValueError
ValueError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/Tadhg/Documents/test.py", line 13, in test_chain
check_raise(test)
File "/Users/Tadhg/Documents/test.py", line 10, in check_raise
raise err from original_error
RuntimeError: test encountered an error during call to __main__.check_raise
the traceback for the error is shown above.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/Tadhg/Documents/test.py", line 18, in <module>
check_raise(test_chain)
File "/Users/Tadhg/Documents/test.py", line 10, in check_raise
raise err from original_error
RuntimeError: test_chain encountered an error during call to __main__.check_raise
the traceback for the error is shown above.
Yes it is long but it is significantly more informative then:
Traceback (most recent call last):
File "/Users/Tadhg/Documents/test.py", line 18, in <module>
check_raise(test_chain)
RuntimeError: An exception was raised.
not to mention that the original error is still usable even if the program doesn't end:
import traceback
def check_raise(function):
...
def fail():
raise ValueError
try:
check_raise(fail)
except RuntimeError as e:
cause = e.__cause__
print("check_raise failed because of this error:")
traceback.print_exception(type(cause), cause, cause.__traceback__)
print("and the program continues...")
I understand 'Don't do this'. On the other hand, there may be some special use cases i believe. I'm generating own errors (just deleting some defined frames...) this way
def get_traceback_with_removed_frames_by_line_string(lines):
"""In traceback call stack, it is possible to remove particular level defined by some line content.
Args:
lines (list): Line in call stack that we want to hide.
Returns:
string: String traceback ready to be printed.
"""
exc = trcbck.TracebackException(*sys.exc_info())
for i in exc.stack[:]:
if i.line in lines:
exc.stack.remove(i)
return "".join(exc.format())
I return just string.
If there is concrete function that is raising, you can add it to ignored frames.
Though have in mind, that if you hide something, somebody may not understand why is something happening...
My use case was to hide only first level - decorator from my library that was decorating all user functions in framework, so error from user side was on level 1.

Show only first and last line from traceback

I've build a little internal DSL with python. I'm using assert vor validation. If the end user types a wrong parameter the dsl should report whats wrong. At the moment this looks like this:
Traceback (most recent call last):
File "tests/maskedtimefilter_test/FilterDSL_test.py", line 63, in test_dsl_validation
input(0): self.regular
File "/Users/sh/reetz/pythonpath/maskedtimedata/maskedtimefilter.py", line 885, in __lshift__
kwargs = self.dsl_validation(kwargs)
File "/Users/sh/reetz/pythonpath/maskedtimedata/maskedtimefilter.py", line 1483, in dsl_validation
check_if_valid(parameter)
File "/Users/sh/reetz/pythonpath/maskedtimedata/dsl.py", line 47, in kernel_a
def kernel_a (x): assert isinstance(x, (list, tuple, np.ndarray)), "kernel must be a list."
AssertionError: kernel must be a list.
But the end users are engineers and no computer scientists. Therefore a minimal Traceback is handy. Is it possible to shrink the Traceback to the essential information (where is the failure and what's the cause) like this?:
Traceback (most recent call last):
File "tests/maskedtimefilter_test/FilterDSL_test.py", line 63, in test_dsl_validation
input(0): self.regular
AssertionError: kernel must be a list.
Reluctantly I'd like to use normal prints!
Why not return the traceback data as an array and just work back from that?
import traceback
try:
codethatwillthrowanexception()
except:
exceptiondata = traceback.format_exc().splitlines()
exceptionarray = [exceptiondata[-1]] + exceptiondata[1:-1]
Somewhere in your call stack you can do this:
try:
my_function_that_can_raise_exception()
except Exception: #or AssertionError
import traceback
traceback.print_exc(limit=1)
limit is the depth of how many stack trace entries you want to show. (in your case, 1)
demo:
In [21]: def f():
...: assert False == True, 'Assertion!'
...:
In [22]: try:
...: f()
...: except Exception:
...: import traceback
...: traceback.print_exc(limit=1)
...:
Traceback (most recent call last):
File "<ipython-input-22-78f9db8d5188>", line 2, in <module>
f()
AssertionError: Assertion!
read more at traceback docs.
As the first part of the traceback is just what you called, you could so something simple, like:
try:
input(0): self.regular # base call
except AssertionError as ae: # catch the exception
print(ae) # print out the message
You can print out whatever you think would be useful in the except block.
Alternatively, you could do something more complex with the traceback module.

Categories

Resources