From 68590e57fef0c46f82714de80382231dff9eccb4 Mon Sep 17 00:00:00 2001 From: Christos Kotsis Date: Fri, 16 Apr 2021 10:29:23 +0300 Subject: [PATCH] [lensesio/exceptions] raise exceptions rather exiting Issue: Lenses is a module and hence shoud raise exceptions rather exiting and causing the whole application to exit Reporter: https://github.com/lensesio/lenses-python/issues/56 Fix: switch all lines of lensesio.lenses.main from print&exit to raise lenses_exception(e) --- lensesio/core/exception.py | 10 ++++-- lensesio/lenses.py | 71 ++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 41 deletions(-) diff --git a/lensesio/core/exception.py b/lensesio/core/exception.py index 95aa7c2..f44b080 100644 --- a/lensesio/core/exception.py +++ b/lensesio/core/exception.py @@ -1,6 +1,10 @@ #!/usr/bin/env python3 -class lenses_exception(Exception): - def __init__(self, error): - self.error = error +class lenses_exception(BaseException): + def __init__(self, value): + self.value = value + + def __str__(self): + return(repr(self.value)) + \ No newline at end of file diff --git a/lensesio/lenses.py b/lensesio/lenses.py index ed9af06..a87a9bc 100644 --- a/lensesio/lenses.py +++ b/lensesio/lenses.py @@ -18,7 +18,6 @@ from lensesio.data.policy import Policy from lensesio.data.sql import SQLExec from sys import modules as sys_mods -from sys import exit import platform @@ -44,7 +43,7 @@ class main( Basic, KafkaTopic, SchemaRegistry, SQLExec, KafkaQuotas, Policy, DataProcessor, DataConnector, - KafkaACL, LensesFlows, lenses_exception, + KafkaACL, LensesFlows, DataConsumers, Topology, AdminPanel, SetupPulsar, ): def __init__( @@ -62,32 +61,32 @@ def __init__( self.active_threads = active_threads - try: - if auth_type not in ['basic', 'service', 'krb5']: - print(''' - Parameters: - Mandatory: - auth_type=basic/krb5/service - url=lenses endpoint - Optional: - username ( - if auth_type is basic - ) - password ( - if username was defined - ) - service_account ( - if auth_type is basic - ) - krb_service ( - if auth_type is krb5 and platform - is either one of linux, darwin - ) - ''') - exit(1) - except NameError: - print("Please provide auth_type [basic, krb5, service]") - exit(1) + if auth_type not in ['basic', 'service', 'krb5']: + raise lenses_exception(''' + Parameters: + Mandatory: + auth_type=basic/krb5/service + url=lenses endpoint + Optional: + username ( + if auth_type is basic + ) + password ( + if username was defined + ) + service_account ( + if auth_type is basic + ) + krb_service ( + if auth_type is krb5 and platform + is either one of linux, darwin + ) + ''') + + if url is None: + raise lenses_exception("URL can not be empty") + elif not url.startswith("http://") and not url.startswith("https://"): + raise lenses_exception("URL Schema is missing. Please provide the schema also: http(s)://example.com") self.auth_type = auth_type self.url = url @@ -100,22 +99,19 @@ def __init__( self.serviceConnect() elif self.auth_type == 'krb5': if platform.system().lower() not in ['linux', 'linux2', 'darwin']: - msg = "Error: gssapi kerberos integration is not supported for " - print(msg + platform.system()) - exit(1) - + raise lenses_exception( + "Error: gssapi kerberos integration is not supported for " + platform.system() + ) try: from lensesio.core.krb_auth import krb5 self.krb5 = krb5 self.krb5.__init__(self, url=url, service=krb_service) self.krb5.KrbAuth(self) except NameError: - print("Kerberos client lib is not installed") - return None + raise lenses_exception("Kerberos client lib is not installed") if self.ConnectionValidation() == 1: - print("Could not login to lenses. Please check the auth options") - exit(1) + raise lenses_exception("Could not login to lenses. Please check the auth options") AdminPanel.__init__(self, verify_cert=verify_cert) Topology.__init__(self, verify_cert=verify_cert) @@ -133,5 +129,4 @@ def InitPulsarClient(self, host, **kwargs): try: self.Pulsar = SetupPulsar.__init__(self, active_threads, host) except NameError: - print("Pulsar client lib is not installed") - return None + raise lenses_exception("Pulsar client lib is not installed")