{"id":8218,"library":"http-exceptions","title":"HTTP Exceptions","description":"http-exceptions is a Python library that provides a set of raisable HTTP status code exceptions, making it easy to integrate standardized HTTP error handling into web applications and APIs. It's currently at version 0.2.10 and sees active maintenance with frequent minor releases focusing on features, fixes, and build improvements.","status":"active","version":"0.2.10","language":"en","source_language":"en","source_url":"https://github.com/DeveloperRSquared/http-exceptions","tags":["http","exceptions","error-handling","api","web"],"install":[{"cmd":"pip install http-exceptions","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for Python versions older than 3.8 to manage package metadata.","package":"importlib-metadata","optional":true}],"imports":[{"symbol":"HTTPException","correct":"from http_exceptions import HTTPException"},{"symbol":"BadRequest","correct":"from http_exceptions import BadRequest"},{"symbol":"NotFound","correct":"from http_exceptions import NotFound"},{"symbol":"Forbidden","correct":"from http_exceptions import Forbidden"},{"symbol":"Unauthorized","correct":"from http_exceptions import Unauthorized"},{"symbol":"InternalServerError","correct":"from http_exceptions import InternalServerError"}],"quickstart":{"code":"from http_exceptions import BadRequest, NotFound, HTTPException\n\ndef get_user_data(user_id: str):\n    if not user_id.isdigit():\n        raise BadRequest(detail=\"User ID must be a numeric string.\")\n    if user_id == \"404\":\n        raise NotFound(detail=f\"User with ID '{user_id}' not found.\")\n    if user_id == \"500\":\n        raise HTTPException(status_code=500, detail=\"Internal server error processing user.\")\n    return {\"id\": user_id, \"name\": f\"User {user_id}\"}\n\n# Example usage and error handling\ntry:\n    user = get_user_data(\"abc\")\nexcept BadRequest as e:\n    print(f\"Caught error: {e.status_code} - {e.detail}\")\n\ntry:\n    user = get_user_data(\"404\")\nexcept NotFound as e:\n    print(f\"Caught error: {e.status_code} - {e.detail}\")\n\ntry:\n    user = get_user_data(\"500\")\nexcept HTTPException as e:\n    print(f\"Caught generic HTTP error: {e.status_code} - {e.detail}\")\n\ntry:\n    user = get_user_data(\"123\")\n    print(f\"Successfully retrieved user: {user}\")\nexcept HTTPException:\n    pass # Should not happen here","lang":"python","description":"This quickstart demonstrates how to define and raise HTTP exceptions within a function and then catch specific exception types to handle different HTTP error scenarios. All custom HTTP exceptions inherit from `HTTPException`."},"warnings":[{"fix":"Always provide a `detail` string argument when instantiating any `http-exceptions` exception, e.g., `raise BadRequest(detail=\"Invalid input.\")`.","message":"When raising `HTTPException` or any of its subclasses, the `detail` argument is mandatory. Omitting it will result in a `TypeError` as it's not optional.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `importlib-metadata` is installed if you are using Python 3.7 or earlier: `pip install importlib-metadata`.","message":"For Python versions prior to 3.8, `importlib-metadata` is a required dependency. While `pip install http-exceptions` should handle this automatically, issues might arise in constrained environments or older `pip` versions.","severity":"gotcha","affected_versions":"< 0.2.9 (was missing), >= 0.2.9 (explicitly added)"},{"fix":"Always use `from http_exceptions import ...` for imports and `pip install http-exceptions` for installation.","message":"The library name on PyPI (`http-exceptions`) differs slightly from the import path (`http_exceptions`). Be careful with underscores vs. hyphens.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify the package is installed with `pip install http-exceptions` and that the import uses an underscore: `from http_exceptions import HTTPException`.","cause":"Incorrect library name used in the import statement, or the package was not installed.","error":"ModuleNotFoundError: No module named 'http_exceptions'"},{"fix":"Provide a descriptive string for the `detail` argument: `raise BadRequest(detail='User provided invalid data.')`.","cause":"Attempted to raise an HTTP exception without providing the mandatory `detail` argument.","error":"TypeError: __init__() missing 1 required positional argument: 'detail'"},{"fix":"Ensure you are catching `HTTPException` or a specific subclass and not a generic `Exception` if you intend to access `status_code` or `detail` attributes. Example: `except HTTPException as e:`.","cause":"This error is unlikely with `http-exceptions` as `status_code` is always present. More likely, you're catching a generic `Exception` which isn't an `HTTPException` or a subclass.","error":"AttributeError: 'HTTPException' object has no attribute 'status_code'"}]}