Repository: httpie/httpie
File: httpie/core.py (Python)

Code before:
    Return exit status code.

    """
    program_name, *args = args
    env.program_name = os.path.basename(program_name)
    args = decode_raw_args(args, env.stdin_encoding)
    plugin_manager.load_installed_plugins()

    from .cli.definition import parser

    if env.config.default_options:
        args = env.config.default_options + args

    include_debug_info = '--debug' in args
    include_traceback = include_debug_info or '--traceback' in args

    if include_debug_info:
        print_debug_info(env)
        if args == ['--debug']:
            return ExitStatus.SUCCESS

    exit_status = ExitStatus.SUCCESS

    try:
        parsed_args = parser.parse_args(args=args, env=env)
        exit_status = program(args=parsed_args, env=env)
    except KeyboardInterrupt:
        env.stderr.write('\n')
        if include_traceback:
            raise
        exit_status = ExitStatus.ERROR_CTRL_C
    except SystemExit as e:
        if e.code != ExitStatus.SUCCESS:
            env.stderr.write('\n')
            if include_traceback:
                raise
            exit_status = ExitStatus.ERROR
    except requests.Timeout:
        exit_status = ExitStatus.ERROR_TIMEOUT
        env.log_error(f'Request timed out ({parsed_args.timeout}s).')
    except requests.TooManyRedirects:
        exit_status = ExitStatus.ERROR_TOO_MANY_REDIRECTS
        env.log_error(
            f'Too many redirects'
            f' (--max-redirects={parsed_args.max_redirects}).'
        )
    except Exception as e:
        # TODO: Further distinction between expected and unexpected errors.
        msg = str(e)
        if hasattr(e, 'request'):
            request = e.request

Review comment (refactor):
`parse_args()` and `program()` each have their own `try` clause intentionally because we handle different types of exceptions for them. For example, we don’t handle base `Exception`s from `parse_args()` as you can see. We also want to keep the `try` branches minimal and the distinction explicit. To get rid of the duplicate handling of `SystemError` and `KeyboardInterrupt`, something like this would be more in line with the intended behavior:

```python
try:
    parse_args()
    try:
        program()
    except Timeout:
        pass
    except TooManyRedirects:
        pass
    except Exception:
        pass
except KeyboardInterrupt:
    pass
except SystemExit:
    pass
```


Code after:
    Return exit status code.

    """
    program_name, *args = args
    env.program_name = os.path.basename(program_name)
    args = decode_raw_args(args, env.stdin_encoding)
    plugin_manager.load_installed_plugins()

    from .cli.definition import parser

    if env.config.default_options:
        args = env.config.default_options + args

    include_debug_info = '--debug' in args
    include_traceback = include_debug_info or '--traceback' in args

    if include_debug_info:
        print_debug_info(env)
        if args == ['--debug']:
            return ExitStatus.SUCCESS

    exit_status = ExitStatus.SUCCESS

    try:
        parsed_args = parser.parse_args(
            args=args,
            env=env,
        )
    except KeyboardInterrupt:
        env.stderr.write('\n')
        if include_traceback:
            raise
        exit_status = ExitStatus.ERROR_CTRL_C
    except SystemExit as e:
        if e.code != ExitStatus.SUCCESS:
            env.stderr.write('\n')
            if include_traceback:
                raise
            exit_status = ExitStatus.ERROR
    else:
        try:
            exit_status = program(
                args=parsed_args,
                env=env,
            )
        except KeyboardInterrupt:
            env.stderr.write('\n')
            if include_traceback:
                raise
            exit_status = ExitStatus.ERROR_CTRL_C
        except SystemExit as e: