{"id":21091004,"url":"https://github.com/tomgrin10/swap-exceptions","last_synced_at":"2025-08-18T17:08:48.730Z","repository":{"id":57472618,"uuid":"289321665","full_name":"tomgrin10/swap-exceptions","owner":"tomgrin10","description":"Python utility decorator and context manager for swapping exceptions","archived":false,"fork":false,"pushed_at":"2020-08-28T14:40:46.000Z","size":41,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-29T11:37:58.270Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tomgrin10.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-21T16:59:45.000Z","updated_at":"2023-09-30T20:52:29.000Z","dependencies_parsed_at":"2022-08-31T07:12:06.078Z","dependency_job_id":null,"html_url":"https://github.com/tomgrin10/swap-exceptions","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/tomgrin10/swap-exceptions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomgrin10%2Fswap-exceptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomgrin10%2Fswap-exceptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomgrin10%2Fswap-exceptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomgrin10%2Fswap-exceptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomgrin10","download_url":"https://codeload.github.com/tomgrin10/swap-exceptions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomgrin10%2Fswap-exceptions/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271027687,"owners_count":24687082,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T21:42:34.556Z","updated_at":"2025-08-18T17:08:48.694Z","avatar_url":"https://github.com/tomgrin10.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swap-exceptions\n\n[![PyPI](https://img.shields.io/pypi/v/swap-exceptions)](https://pypi.org/project/swap-exceptions/)\n[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/swap-exceptions)](https://pypi.org/project/swap-exceptions/)\n[![PyPI License](https://img.shields.io/pypi/l/swap-exceptions)](https://pypi.org/project/swap-exceptions/)\n[![Code Style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black/)\n\nPython utility decorator and context manager for swapping exceptions.\n\n### Basic Usage\n\nAs a decorator:\n```python\nfrom swap_exceptions import swap_exceptions\n\n@swap_exceptions({KeyError: ValueError(\"Incorrect value\")})\ndef get_value(key: str):\n    d = {'a': 1, 'b': 2}\n    return d[key]\n\nget_value('c')  # ValueError: Incorrect value\n```\n\nOr as a context manager:\n```python\nfrom swap_exceptions import swap_exceptions\n\ndef get_value(key: str):\n    d = {'a': 1, 'b': 2}\n    with swap_exceptions({KeyError: ValueError(\"Incorrect value\")}):\n        return d[key]\n\nget_value('c')  # ValueError: Incorrect value\n```\n\n### Advanced Usage\n\nMapping key can also be a tuple:\n```python\nfrom swap_exceptions import swap_exceptions\n\n@swap_exceptions({(KeyError, TypeError): ValueError(\"Incorrect value\")})\ndef get_value(key: str):\n    d = {'a': 1, 'b': 2, 'c': 'not a number'}\n    return d[key] + 10\n\nget_value('c')  # ValueError: Incorrect value\n```\n\nMapping value can also be a factory that generates the exception:\n```python\nfrom swap_exceptions import swap_exceptions\n\n@swap_exceptions({KeyError: lambda e: ValueError(f\"Incorrect value {e.args[0]}\")})\ndef get_value(key: str):\n    d = {'a': 1, 'b': 2}\n    return d[key]\n\nget_value('c')  # ValueError: Incorrect value c\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomgrin10%2Fswap-exceptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomgrin10%2Fswap-exceptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomgrin10%2Fswap-exceptions/lists"}