Non-zero exit code on error

This changes all critical errors to raise exceptions. This allows to
have a single critical error handler that sets exit code to non-zero.

This way loconotion can be better used in automation jobs, where
loconotion failure should stop subsequent tasks.
This commit is contained in:
Martynas Mickevičius 2022-03-19 13:30:03 +02:00
parent 46c77076ab
commit b4bc861e16
No known key found for this signature in database
GPG Key ID: E735DF276C508071
3 changed files with 41 additions and 34 deletions

View File

@ -3,6 +3,12 @@ import sys
import modules.main as main
def _exit():
try:
sys.exit(1)
except SystemExit:
os._exit(1)
if __name__ == "__main__":
try:
args = main.get_args()
@ -11,7 +17,10 @@ if __name__ == "__main__":
parser.run()
except KeyboardInterrupt:
log.critical("Interrupted by user")
try:
sys.exit(0)
except SystemExit:
os._exit(0)
_exit()
except Exception as ex:
if args.verbose:
log.exception(ex)
else:
log.critical(f"{ex.__class__.__name__}: {str(ex)}")
_exit()

View File

@ -16,7 +16,7 @@ try:
except ModuleNotFoundError as error:
log.critical(f"ModuleNotFoundError: {error}. Have you installed the requirements?")
sys.exit()
sys.exit(1)
def get_args():
@ -121,32 +121,30 @@ def setup_logging(args):
def init_parser(args, log):
# initialise the website parser
try:
if urllib.parse.urlparse(args.target).scheme:
try:
requests.get(args.target)
except requests.ConnectionError as exception:
log.critical("Connection error")
if "notion.so" in args.target or "notion.site" in args.target:
log.info("Initialising parser with simple page url")
config = {"page": args.target}
parser = Parser(config=config, args=vars(args))
else:
log.critical(f"{args.target} is not a notion.so page")
elif Path(args.target).is_file():
with open(args.target, encoding="utf-8") as f:
parsed_config = toml.loads(f.read())
log.info("Initialising parser with configuration file")
log.debug(parsed_config)
parser = Parser(config=parsed_config, args=vars(args))
if urllib.parse.urlparse(args.target).scheme:
try:
requests.get(args.target)
except requests.ConnectionError as exception:
log.critical("Connection error")
raise exception
if "notion.so" in args.target or "notion.site" in args.target:
log.info("Initialising parser with simple page url")
config = {"page": args.target}
parser = Parser(config=config, args=vars(args))
else:
log.critical(f"Config file {args.target} does not exist")
log.critical(f"{args.target} is not a notion.so page")
raise Exception()
except FileNotFoundError as e:
log.critical(f"FileNotFoundError: {e}")
sys.exit(0)
elif Path(args.target).is_file():
with open(args.target, encoding="utf-8") as f:
parsed_config = toml.loads(f.read())
log.info("Initialising parser with configuration file")
log.debug(parsed_config)
parser = Parser(config=parsed_config, args=vars(args))
else:
log.critical(f"Config file {args.target} does not exist")
raise FileNotFoundError(args.target)
return parser

View File

@ -26,7 +26,7 @@ try:
cssutils.log.setLevel(logging.CRITICAL) # removes warning logs from cssutils
except ModuleNotFoundError as error:
log.critical(f"ModuleNotFoundError: {error}. have your installed the requirements?")
sys.exit()
sys.exit(1)
from .conditions import notion_page_loaded, toggle_block_has_opened
@ -42,7 +42,7 @@ class Parser:
" make sure it contains a 'page' key with the url of the notion.site"
" page to parse"
)
return
raise Exception()
# get the site name from the config, or make it up by cleaning the target page's slug
site_name = self.config.get("name", self.get_page_slug(index_url, extension=False))
@ -222,7 +222,7 @@ class Parser:
" https://chromedriver.chromium.org/downloads and use the"
" --chromedriver argument to point to the chromedriver executable"
)
sys.exit()
raise exception
log.info(f"Initialising chromedriver at {chromedriver_path}")
logs_path = Path.cwd() / ".logs" / "webdrive.log"
@ -259,12 +259,12 @@ class Parser:
try:
self.load_correct_theme(url)
except TimeoutException:
except TimeoutException as ex:
log.critical(
"Timeout waiting for page content to load, or no content found."
" Are you sure the page is set to public?"
)
return
raise ex
self.scroll_to_the_bottom()