Skip to content

Commit

Permalink
Fix crash on incorrect links
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDokuchaev committed Jun 25, 2024
1 parent 76ecefc commit 912eabc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
7 changes: 6 additions & 1 deletion md_dead_link_check/link_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
MSG_PATH_NOT_FOUND = "Path not found"
MSG_PATH_NOT_ADDED = "Path not added to repository"
MSG_FRAGMENT_NOT_FOUND = "Fragment not found"
MSG_UNKNOWN_ERROR = "Unknown error"


@dataclass
Expand Down Expand Up @@ -74,7 +75,11 @@ async def process_link(link: str, session: ClientSession, config: Config) -> Lin
if TIMEOUT_RESPONSE_CODE in config.catch_response_codes:
return LinkStatus(link, err_msg=MSG_TIMEOUT)
return LinkStatus(link, warn_msg=MSG_TIMEOUT)

except Exception as e:
msg = str(e)
if not msg:
msg = MSG_UNKNOWN_ERROR
return LinkStatus(link, err_msg=msg)
return LinkStatus(link)


Expand Down
34 changes: 28 additions & 6 deletions tests/test_link_cheker.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def test_fails():
md_data = {path: process_md_file(Path(path), root_dir)}
ret = check_all_links(md_data, Config(), root_dir, list(md_data.keys()), TEST_FILES)

# Differ err_msg on local test and github-ci
ret[1].err_msg = ""
# Output message depends on proxy settings
ret[1].err_msg = None
ret[1].warn_msg = None

ref = [
StatusInfo(
Expand All @@ -52,7 +53,8 @@ def test_fails():
location=Path("tests/test_md_files/fail.md"),
line_num=4,
),
err_msg="",
err_msg=None,
warn_msg=None,
),
StatusInfo(
link_info=LinkInfo(link="/test/fail.md1", location=Path("tests/test_md_files/fail.md"), line_num=8),
Expand Down Expand Up @@ -80,6 +82,15 @@ def test_fails():
err_msg="Path not found",
warn_msg=None,
),
StatusInfo(
link_info=LinkInfo(
link="error://urls/",
location=Path("tests/test_md_files/fail.md"),
line_num=17,
),
err_msg="Unknown error",
warn_msg=None,
),
]
assert ret == ref

Expand Down Expand Up @@ -112,15 +123,17 @@ def test_exclude_links(exclude_links):
TEST_FILES,
)

# Differ err_msg on local test and github-ci
ret[0].err_msg = ""
# Output message depends on proxy settings
ret[0].err_msg = None
ret[0].warn_msg = None

ref = [
StatusInfo(
link_info=LinkInfo(
link="https://not_exist_github.githubcom/", location=Path("tests/test_md_files/fail.md"), line_num=4
),
err_msg="",
err_msg=None,
warn_msg=None,
),
StatusInfo(
link_info=LinkInfo(
Expand All @@ -140,5 +153,14 @@ def test_exclude_links(exclude_links):
err_msg="Path not found",
warn_msg=None,
),
StatusInfo(
link_info=LinkInfo(
link="error://urls/",
location=Path("tests/test_md_files/fail.md"),
line_num=17,
),
err_msg="Unknown error",
warn_msg=None,
),
]
assert ret == ref
2 changes: 2 additions & 0 deletions tests/test_md_files/fail.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
[1](/tests/test_md_files/fail.md#fail)

[1](not_exist_dir)

[1](error://urls/)

0 comments on commit 912eabc

Please sign in to comment.