Closed moracabanas closed 1 year ago
Well, you're a self-admitted noob, so take this advice first: It's really much easier if you Google your question first :)
This is the first answer for "python disable traceback", and by the sounds of things it's exactly what you need: https://stackoverflow.com/questions/27674602/hide-traceback-unless-a-debug-flag-is-set
Then you can check the docs about how to add a boolean value (flag) to your typer CLI afterwards :)
Thanks for posting. Not gonna lie I am a little bit mad about your response because, I am doing this to learn, and believe me, I have spent several days crawling the web to learn and understand a proper way (if any) to work with python environmental specific error logging. Still unclear I make an elaborated and humble question because my experience is low compared to other developers. Because I am specifically able to ask questions here, my wish is to find someone more experience than me on this topic, which could point me to the right direction. All the stackoverflow solutions I have tried were so unclear or didn't work at all. The fact I am here asking the community in the most humble form I can speak is just because I give up on stackoverflow trying to disable and understand trackbacks and neither solution was clear for me. So, please given an elaborated question, just to marking out the guy asking is a noob and pointing to the first google result could be an improper way to help people to grow as developers. Also this kind of situations leads people to avoid looking for help. Note that I am not disregarding your help try, I am just saying, when a developer is looking for help to get deeper insights on advanced language core topics there is manners and etiquette about it. I am just noob at python I am educated on google searching and other programming topics.. Blaming people's lack of experience when they are looking for help in specific topic with no lazy posting is unexpected by any open source community means. Hope you understand
Hi, sorry that you felt offended, that was not intended. But it is just a fact that Google result number 1 had your answer. I understand if you maybe didn't use those search terms though.
Regarding whether it works or not - it does, I tested it. You do need to set the excepthook (when using rich), because it is already overridden to not respect the tracebacklimit variable.
I wish you the best on your Python learning journey.
@moracabanas there's an undocumented feature in Rich to hide tracebacks below a certain level, by putting a variable _rich_traceback_guard
with a boolean value (True
or False
).
You can see how it's used internally here: https://github.com/tiangolo/typer/blob/master/typer/main.py#L675
Maybe that can give you a hint about what to explore and play with. :nerd_face:
First Check
Commit to Help
Example Code
Description
I am noob at python trying to write a Portainer CLI client inspired by docker CLI, for learning purposes and speed up personal deployments.
The code shown is just my first try at create a persisted login based on a .jwt local file once a succesful 200 response (and etc... future checks)
Obviously there are tons of unhandled errors and you might notice I am a completely uneducated illiterate when it comes to error handling. My application will burst in imaginative ways I am not even prepared to unit test them
Anyway, when I am checking the login() flow, I test it for breaking inputs like bad Url, bad credentials etc... Then I got several raised errors from
requests
or whatever imported lib which is giving up:I will pack this CLI with
pyinstaller
hiding its implementation and most important thing, hiding stressful tracebacks for the average user, preserving usefull error like the last raised one:I have searched for the correct way to preserve useful errors for the user, hiding the tracebacks and I have come across
sys.excepthook
exception handlers with no luck.Tracebacks are enabled with any code hack I've tried. I am a bit lost with providing a usefull error handling for the user here.
Would be awesome if someone more experience could point me to the right direction with useful tips about this
Operating System
Linux, Windows
Operating System Details
Windows 11 WSL2 Poetry
Typer Version
extras = ["all"], version = "^0.7.0"
Python Version
3.11.0
Additional Context
No response