Skip to content

Commit

Permalink
catch_response_codes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Mar 4, 2024
1 parent 5650a94 commit 8e39676
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ File: tests/test_md_files/fail.md:13 • Link: a.md#fail • Error: Not found fr
```

> [!NOTE]
> Only error codes like **404 (Not Found)**, **410 (Gone)**, and **500 (Internal Server Error)**,
> Error codes like **404 (Not Found)**, **408 (Timeout)**, **410 (Gone)**, and **500 (Internal Server Error)**,
> and links that don't exist are considered "dead links". Other error codes typically indicate
> temporary issues with the host server or unsupported links for the HEAD request type.
Expand Down Expand Up @@ -90,9 +90,11 @@ This tool seamlessly integrates with your project's `pyproject.toml` file for co
To leverage a different file, invoke the `--config` option during execution.

- timeout: Specifies the maximum time (in seconds) to wait for web link responses. Default: `5` seconds.
- exclude_links: Accepts a list of links to exclude from checks. Default: `[]`.
- exclude_files: Accepts a list of files to exclude from checks. Default: `[]`.
- force_get_requests_for_links: Accepts a list of links for which the tool will use `GET` requests during checks. Default: `[]`.
- catch_response_codes: List of HTTP response codes to consider as failures.
If empty, all codes greater than 400 will be marked as failures. Default: `[404, 410, 500]`.
- exclude_links: List of links to exclude from checks. Default: `[]`.
- exclude_files: List of files to exclude from checks. Default: `[]`.
- force_get_requests_for_links: List of links for which the tool will use `GET` requests during checks. Default: `[]`.
- check_web_links: Toggle web link checks on or off. Default: `true`.
- validate_ssl: Toggles whether to validate SSL certificates when checking web links. Default: `true`.

Expand Down
7 changes: 7 additions & 0 deletions md_dead_link_check/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@

PROJECT_NAME = "md_dead_link_check"

DEFAULT_CATCH_RESPONSE_CODES = [
404, # Not found
410, # Gone
500, # Internal Server Error (for cannot connect to host under proxy)
]


@dataclass
class Config:
timeout: int = 5
catch_response_codes: List[int] = field(default_factory=lambda: DEFAULT_CATCH_RESPONSE_CODES)
exclude_links: List[str] = field(default_factory=lambda: [])
exclude_files: List[str] = field(default_factory=lambda: [])
force_get_requests_for_links: List[str] = field(default_factory=lambda: [])
Expand Down
12 changes: 5 additions & 7 deletions md_dead_link_check/link_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
from md_dead_link_check.preprocess import LinkInfo
from md_dead_link_check.preprocess import MarkdownInfo

CATCH_RESPONSE_STATUS = [
404, # Not found
410, # Gone
500, # Internal Server Error (for cannot connect to host under proxy)
]
TIMEOUT_RESPONSE_CODE = 408


@dataclass
Expand Down Expand Up @@ -53,15 +49,17 @@ async def process_link(link_info: LinkInfo, session: ClientSession, config: Conf

response.raise_for_status()
except ClientResponseError as e:
if e.status in CATCH_RESPONSE_STATUS:
if not config.catch_response_codes or e.status in config.catch_response_codes:
return StatusInfo(link_info, f"{e.status}: {e.message}")
return StatusInfo(link_info, warn_msg=f"{e.status}: {e.message}")
except asyncio.CancelledError as e:
return StatusInfo(link_info, str(e))
except ClientConnectorError as e:
return StatusInfo(link_info, str(e))
except asyncio.TimeoutError:
return StatusInfo(link_info, warn_msg="TimeoutError")
if TIMEOUT_RESPONSE_CODE in config.catch_response_codes:
return StatusInfo(link_info, err_msg="408: Timeout")
return StatusInfo(link_info, warn_msg="408: Timeout")

return StatusInfo(link_info)

Expand Down

0 comments on commit 8e39676

Please sign in to comment.