diff --git a/docs/contributors/fetch.py b/docs/contributors/fetch.py index ba94c281..b1dc576a 100644 --- a/docs/contributors/fetch.py +++ b/docs/contributors/fetch.py @@ -207,12 +207,12 @@ def fetch(url: str, params: Optional[Dict[str, str]] = None) -> UserInfo: xrate_limit_reset = int(exc.response.headers['X-RateLimit-Reset']) wait = xrate_limit_reset - now if wait > 20: - raise FinishedForNow() + raise FinishedForNow() from exc debug(' !', 'Waiting', wait, 'seconds before another try ...') sleep(wait) continue return req.json() - assert ValueError('Rate limit exceeded') + raise ValueError('Rate limit exceeded') def new_person(**kwargs: str) -> Person: diff --git a/httpie/cli/argtypes.py b/httpie/cli/argtypes.py index 8f19c3c5..232e8268 100644 --- a/httpie/cli/argtypes.py +++ b/httpie/cli/argtypes.py @@ -219,8 +219,8 @@ def parse_format_options(s: str, defaults: Optional[dict]) -> dict: try: path, value = option.lower().split(':') section, key = path.split('.') - except ValueError: - raise argparse.ArgumentTypeError(f'invalid option {option!r}') + except ValueError as exc: + raise argparse.ArgumentTypeError(f'invalid option {option!r}') from exc if value in value_map: parsed_value = value_map[value] @@ -235,9 +235,8 @@ def parse_format_options(s: str, defaults: Optional[dict]) -> dict: else: try: default_value = defaults[section][key] - except KeyError: - raise argparse.ArgumentTypeError( - f'invalid key {path!r}') + except KeyError as exc: + raise argparse.ArgumentTypeError(f'invalid key {path!r}') from exc default_type, parsed_type = type(default_value), type(parsed_value) if parsed_type is not default_type: @@ -246,7 +245,7 @@ def parse_format_options(s: str, defaults: Optional[dict]) -> dict: f' {value!r} in {option!r}' f' (expected {default_type.__name__}' f' got {parsed_type.__name__})' - ) + ) from exc options[section][key] = parsed_value @@ -262,9 +261,8 @@ PARSED_DEFAULT_FORMAT_OPTIONS = parse_format_options( def response_charset_type(encoding: str) -> str: try: ''.encode(encoding) - except LookupError: - raise argparse.ArgumentTypeError( - f'{encoding!r} is not a supported encoding') + except LookupError as exc: + raise argparse.ArgumentTypeError(f'{encoding!r} is not a supported encoding') from exc return encoding diff --git a/httpie/cli/requestitems.py b/httpie/cli/requestitems.py index 8931b88a..66da70ab 100644 --- a/httpie/cli/requestitems.py +++ b/httpie/cli/requestitems.py @@ -154,7 +154,7 @@ def process_file_upload_arg(arg: KeyValueArg) -> Tuple[str, IO, str]: try: f = open(os.path.expanduser(filename), 'rb') except OSError as e: - raise ParseError(f'{arg.orig!r}: {e}') + raise ParseError(f'{arg.orig!r}: {e}') from e return ( os.path.basename(filename), f, @@ -215,16 +215,16 @@ def load_text_file(item: KeyValueArg) -> str: with open(os.path.expanduser(path), 'rb') as f: return f.read().decode() except OSError as e: - raise ParseError(f'{item.orig!r}: {e}') - except UnicodeDecodeError: + raise ParseError(f'{item.orig!r}: {e}') from e + except UnicodeDecodeError as exc: raise ParseError( f'{item.orig!r}: cannot embed the content of {item.value!r},' ' not a UTF-8 or ASCII-encoded text file' - ) + ) from exc def load_json(arg: KeyValueArg, contents: str) -> JSONType: try: return load_json_preserve_order_and_dupe_keys(contents) except ValueError as e: - raise ParseError(f'{arg.orig!r}: {e}') + raise ParseError(f'{arg.orig!r}: {e}') from e diff --git a/httpie/config.py b/httpie/config.py index 27bc0a78..9cbcf0ce 100644 --- a/httpie/config.py +++ b/httpie/config.py @@ -68,11 +68,9 @@ def read_raw_config(config_type: str, path: Path) -> Dict[str, Any]: try: return json.load(f) except ValueError as e: - raise ConfigFileError( - f'invalid {config_type} file: {e} [{path}]' - ) + raise ConfigFileError(f'invalid {config_type} file: {e} [{path}]') except FileNotFoundError: - pass + raise ConfigFileError(f'cannot read {config_type} file: {path} not found') except OSError as e: raise ConfigFileError(f'cannot read {config_type} file: {e}') diff --git a/httpie/output/utils.py b/httpie/output/utils.py index 875e8855..9129dbea 100644 --- a/httpie/output/utils.py +++ b/httpie/output/utils.py @@ -13,15 +13,15 @@ def load_prefixed_json(data: str) -> Tuple[str, json.JSONDecoder]: # First, the full data. try: return '', load_json_preserve_order_and_dupe_keys(data) - except ValueError: - pass + except ValueError as exc: + raise ValueError('Invalid JSON') from exc # Then, try to find the start of the actual body. data_prefix, body = parse_prefixed_json(data) try: return data_prefix, load_json_preserve_order_and_dupe_keys(body) - except ValueError: - raise ValueError('Invalid JSON') + except ValueError as exc: + raise ValueError('Invalid JSON') from exc def parse_prefixed_json(data: str) -> Tuple[str, str]: