mercredi 16 août 2023

Is it a bad practice to use python sys.exit in small command line tools? [closed]

In my group there was this idea that we should replace all sys.exit() with an exception and have a global try catch in main/entry-point function that will catch all these exceptions.

We used sys.exit() for cases when the application encountered a fatal issue and cannot continue. So in that case we logged an error message and exited the application.

Now, my colleagues are severely worried about sys.exit() usage and want to remove them by making the code, to me, more complex - less robust.

An in general Is it a bed practice to use sys.exit() in python in command line tools?

so here is what we have:

def func():
    try:
        response = get_some_google_service_response()
    except GoogleError as error:
        log.fata(f"We encountered a fatal error: {error} and cannot continue. Please contact our team here-here-here")
        sys.exit(1)

    use_that(response)
    rely_on(response)
    etc()


def main():
    func()

if __name__ == "__main__":
    main()
             

and here is what they want to change this to:

def func():
    try:
        response = get_some_google_service_response()
    except GoogleError as error:
         raise CustomGoogleError("We encountered a fatal error: {error} and cannot continue. Please contact our team here-here-here")

    use_that(response)
    rely_on(response)
    etc()


def main():
    try:
        func()
    except Exception as error:
        log.error(error)

if __name__ == "__main__":
    main()

Of course this is very simplified example and our code is 10s of files and many nested function calls and classes.

Please give me your opinion as I think what my colleagues are doing is wrong but want more words to argue this.

Aucun commentaire:

Enregistrer un commentaire