Skip to content

Commit e43f221

Browse files
committed
fix: prevent KeyError when wiki file does not exist in download fallback
When the MediaWiki FileApi returns download-notfound, the exception raised inside the try block was silently caught by the broad `except Exception`, setting web_api_failed=True. The subsequent fallback then crashed with KeyError: 'url' because the file's imageinfo has no URL for non-existent files. Fix by using a flag instead of raising inside the try block.
1 parent 54493b2 commit e43f221

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

‎src/osw/controller/file/wiki.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def get(self) -> IO:
4040
# use web api
4141
full_title = f"{self.namespace}:{self.title}"
4242
web_api_failed = False
43+
file_not_found = False
4344
response = None
4445
try:
4546
url = (
@@ -54,7 +55,7 @@ def get(self) -> IO:
5455
if api_error is not None:
5556
if api_error == "download-notfound":
5657
# File does not exist
57-
raise Exception("File does not exist: " + full_title)
58+
file_not_found = True
5859
elif api_error == "badvalue":
5960
# Extension FileApi not installed on the server
6061
web_api_failed = True
@@ -64,6 +65,8 @@ def get(self) -> IO:
6465

6566
except Exception:
6667
web_api_failed = True
68+
if file_not_found:
69+
raise Exception("File does not exist: " + full_title)
6770
if web_api_failed:
6871
# fallback: use direct download
6972
url = file.imageinfo["url"]

0 commit comments

Comments
 (0)