[QUESTION] Disable traceback globally on production #596
-
First Check
Commit to Help
Example Codeimport typer
import requests
from rich import print
import logging
from rich.logging import RichHandler
logging.basicConfig(
level="DEBUG",
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(rich_tracebacks=True)],
)
log = logging.getLogger("rich")
app = typer.Typer(pretty_exceptions_show_locals=False)
greetings_login = "Login with your Portainer Url, Username and Password to deploy projects to your Portainer i.e.: https://my-portainer.example.com"
@app.command()
def login(
url: str = typer.Option(..., prompt=f'{greetings_login}\nUrl'),
username: str = typer.Option(..., prompt=True),
password: str = typer.Option(..., prompt=True, hide_input=True)
):
url = f'{url}/api/auth'
payload = {
"Username": username,
"Password": password
}
try:
response = requests.request("POST", url, json=payload)
if (response.status_code == 200):
print('[green]Successful login ✅[/green] saving credentials...')
with open("./.jwt", "w") as f:
f.write(response.text)
else:
print(f'Error: {response.reason}')
except Exception as e:
log.exception(e)
@app.command()
def logout(
force: bool = typer.Option(..., prompt="Are you sure you want to log out?"),
):
if force:
print(f"Deleting credentials")
#TODO implement check .jwt exist then delete
else:
print("Operation cancelled")
if __name__ == "__main__":
app() DescriptionI 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...
I will pack this CLI with MissingSchema: Invalid URL 'asdfasdf/api/auth': No scheme supplied. Perhaps you meant http://asdfasdf/api/auth? I have searched for the correct way to preserve useful errors for the user, hiding the tracebacks and I have come across 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 SystemLinux, Windows Operating System DetailsWindows 11 Typer Versionextras = ["all"], version = "^0.7.0" Python Version3.11.0 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
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 :) |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
@moracabanas there's an undocumented feature in Rich to hide tracebacks below a certain level, by putting a variable 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. 🤓 |
Beta Was this translation helpful? Give feedback.
-
@moracabanas
|
Beta Was this translation helpful? Give feedback.
@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
orFalse
).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. 🤓