Skip to content

Instantly share code, notes, and snippets.

@jorge-lavin
Created September 3, 2014 10:12
Show Gist options
  • Save jorge-lavin/b6ef7a67b95ea2e7e605 to your computer and use it in GitHub Desktop.
Save jorge-lavin/b6ef7a67b95ea2e7e605 to your computer and use it in GitHub Desktop.
import sys
import random
import logging
logging.basicConfig(format='[%(levelname)5s] %(message)s',level=logging.DEBUG)
def run(program=''):
"""Function to be wrapped"""
if not program:
raise OSError('Not program specified to run.')
logging.debug('Running program ...')
if program == 'math':
raise OverflowError('Too much arithmetic')
else:
logging.debug('OK')
return program
def verbose_run(error_message, f, *args):
"""Wrapper for run, logs a custom error message if an exception is raised"""
try:
if not args:
return_value = f()
else:
return_value = f(*args)
except (OSError, OverflowError) as f_err:
logging.error(error_message)
logging.error('Logging exception')
#logging.exception(f_err)
raise f_err
def main():
"""docstring for main"""
args = ['', 'math', 'neither blank nor math']
argument_to_launch = args[random.randint(0, len(args)-1)]
try:
verbose_run('Some verbose message', run, argument_to_launch)
except (OSError, OverflowError) as run_err:
logging.debug('Exception catched with verbose_run')
logging.error(run_err)
logging.info('Now normal run')
try:
run(argument_to_launch)
except (OSError, OverflowError) as run_err:
logging.debug('Exception catched with not verbose_run')
logging.error(run_err)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment