Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get a stack trace from raised exception?

Hi I wrote a pretty big framework for quantopian use, and one of those wraps the record() method.

I'd like to show a user-friendly error when someone tries to record() a non-numerical value. (currently the backtest crashes with no useful info)

I can raise an exception for the user, but how can I inform the user of the method calling my wrapper?

For example:

    def record(this, name,value):  
        if maths.isNumber(value)==False:  
            raise Exception("the value you are attempting to record is not a number.  value={0}".format(value))  
        record(**{name:value})  
4 responses

Unfortunately, I don't think we have a way to provide that right now. As you know, we have an interesting security challenge where we let our customers run arbitrary Python on our servers. To make that safe we have to strip out certain functionality, and the ability to see the full stack is one of the things that gets lost.

We have a bit of a skunkworks project going from our summer intern that might do an end-run around this entire problem. I wish I had a good workaround in the interim, but I can't think of one.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

could you perhaps show the line number for as long as the stack is in the user code, and strip out stack information for other files?

Idk if this is still an issue for you, but you could try something like:

try:  
    record(name=value)  
except ValueError as v:  
    print 'Failed to record %s=%s: %s' % (name, value, v)  

This function now will always raise a ValueError in the case of an invalid param. I also suggest that you raise ValueErrors when a user has provided an invalid param to one of your functions as this is more standard, and base Exception is less descriptive and makes it harder to write multiple exception handlers.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

I would like to bring this issue up again. Right now debugging code is made even more difficult by the fact that the exceptions raised point to sections of code far away from the actual cause. This means that I have to go through my algorithm and comment stuff out until I locate the offending code.

Ideally you would wrap every algorithm's execution in a try block, catch the exception, strip out the first 'n' entries of the stack (where 'n' is the number of entries that have to do with systems and platform code; this should be a constant for any given setup), and then present the remaining stack trace to the user. This way people could get something more useful than "KeyError in handle_data".