Skip to content

Commit 2574787

Browse files
Tidy up exception handling
1 parent c80af50 commit 2574787

7 files changed

Lines changed: 96 additions & 61 deletions

File tree

perdoo/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import logging
1212
import os
13+
from functools import cache
1314
from pathlib import Path
1415

1516
from rich.logging import RichHandler
@@ -18,27 +19,31 @@
1819
from perdoo.console import CONSOLE
1920

2021

22+
@cache
2123
def get_cache_root() -> Path:
2224
cache_home = os.getenv("XDG_CACHE_HOME", default=str(Path.home() / ".cache"))
2325
folder = Path(cache_home).resolve() / "perdoo"
2426
folder.mkdir(exist_ok=True, parents=True)
2527
return folder
2628

2729

30+
@cache
2831
def get_config_root() -> Path:
2932
config_home = os.getenv("XDG_CONFIG_HOME", default=str(Path.home() / ".config"))
3033
folder = Path(config_home).resolve() / "perdoo"
3134
folder.mkdir(exist_ok=True, parents=True)
3235
return folder
3336

3437

38+
@cache
3539
def get_data_root() -> Path:
3640
data_home = os.getenv("XDG_DATA_HOME", default=str(Path.home() / ".local" / "share"))
3741
folder = Path(data_home).resolve() / "perdoo"
3842
folder.mkdir(exist_ok=True, parents=True)
3943
return folder
4044

4145

46+
@cache
4247
def get_state_root() -> Path:
4348
data_home = os.getenv("XDG_STATE_HOME", default=str(Path.home() / ".local" / "state"))
4449
folder = Path(data_home).resolve() / "perdoo"
@@ -47,7 +52,7 @@ def get_state_root() -> Path:
4752

4853

4954
def setup_logging(debug: bool = False) -> None:
50-
install(show_locals=True, max_frames=6, console=CONSOLE)
55+
install(show_locals=debug, max_frames=3, console=CONSOLE)
5156

5257
console_handler = RichHandler(
5358
rich_tracebacks=True,

perdoo/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def run(
201201
try:
202202
entries.append(get_archive(file))
203203
except NotImplementedError as nie: # noqa: PERF203
204-
LOGGER.error("%s, Skipping", nie) # noqa: TRY400
204+
LOGGER.error("%s, Skipping", nie)
205205

206206
for index, entry in enumerate(entries):
207207
CONSOLE.rule(

perdoo/metadata/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def read_meta_file(cls: type[PascalModel], filename: str) -> PascalModel | None:
3232
metron_info = cast(MetronInfo, metron_info)
3333
except ValidationError as ve:
3434
if debug:
35-
LOGGER.error("'%s' contains an invalid MetronInfo file: %s", archive.path.name, ve) # noqa: TRY400
35+
LOGGER.error("'%s' contains an invalid MetronInfo file: %s", archive.path.name, ve)
3636
else:
37-
LOGGER.error("'%s' contains an invalid MetronInfo file", archive.path.name) # noqa: TRY400
37+
LOGGER.error("'%s' contains an invalid MetronInfo file", archive.path.name)
3838
comic_info = None
3939
try:
4040
comic_info = read_meta_file(cls=ComicInfo, filename="ComicInfo.xml") or read_meta_file(
@@ -44,7 +44,7 @@ def read_meta_file(cls: type[PascalModel], filename: str) -> PascalModel | None:
4444
comic_info = cast(ComicInfo, comic_info)
4545
except ValidationError as ve:
4646
if debug:
47-
LOGGER.error("'%s' contains an invalid ComicInfo file: %s", archive.path.name, ve) # noqa: TRY400
47+
LOGGER.error("'%s' contains an invalid ComicInfo file: %s", archive.path.name, ve)
4848
else:
49-
LOGGER.error("'%s' contains an invalid ComicInfo file", archive.path.name) # noqa: TRY400
49+
LOGGER.error("'%s' contains an invalid ComicInfo file", archive.path.name)
5050
return metron_info, comic_info

perdoo/services/comicvine.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ def _search_series(self, name: str | None, volume: int | None, year: int | None)
6868
return self._search_series(name=name, volume=volume, year=None)
6969
if Confirm.ask("Search Again", console=CONSOLE):
7070
return self._search_series(name=None, volume=None, year=None)
71-
return None
72-
except ServiceError:
73-
LOGGER.exception("")
74-
return None
71+
except ServiceError as err:
72+
LOGGER.error(err)
7573
except JSONDecodeError:
76-
LOGGER.error("Unable to get response from Comicvine") # noqa: TRY400
77-
return None
74+
LOGGER.error("Unable to get response from Comicvine")
75+
return None
7876

7977
def fetch_series(self, search: SeriesSearch) -> Volume | None:
8078
series_id = search.comicvine or self._search_series(
@@ -86,12 +84,15 @@ def fetch_series(self, search: SeriesSearch) -> Volume | None:
8684
series = self.session.get_volume(volume_id=series_id)
8785
search.comicvine = series_id
8886
return series
89-
except ServiceError:
90-
LOGGER.exception("")
91-
return None
87+
except ServiceError as err:
88+
LOGGER.error(err)
9289
except JSONDecodeError:
93-
LOGGER.error("Unable to get response from Comicvine") # noqa: TRY400
90+
LOGGER.error("Unable to get response from Comicvine")
9491
return None
92+
if search.comicvine:
93+
search.comicvine = None
94+
return self.fetch_series(search=search)
95+
return None
9596

9697
def _search_issue(self, series_id: int, number: str | None) -> int | None:
9798
try:
@@ -121,13 +122,11 @@ def _search_issue(self, series_id: int, number: str | None) -> int | None:
121122
if number:
122123
LOGGER.info("Searching again without the IssueNumber")
123124
return self._search_issue(series_id=series_id, number=None)
124-
return None
125-
except ServiceError:
126-
LOGGER.exception("")
127-
return None
125+
except ServiceError as err:
126+
LOGGER.error(err)
128127
except JSONDecodeError:
129-
LOGGER.error("Unable to get response from Comicvine") # noqa: TRY400
130-
return None
128+
LOGGER.error("Unable to get response from Comicvine")
129+
return None
131130

132131
def fetch_issue(self, series_id: int, search: IssueSearch) -> Issue | None:
133132
issue_id = search.comicvine or self._search_issue(series_id=series_id, number=search.number)
@@ -137,12 +136,15 @@ def fetch_issue(self, series_id: int, search: IssueSearch) -> Issue | None:
137136
issue = self.session.get_issue(issue_id=issue_id)
138137
search.comicvine = issue_id
139138
return issue
140-
except ServiceError:
141-
LOGGER.exception("")
142-
return None
139+
except ServiceError as err:
140+
LOGGER.error(err)
143141
except JSONDecodeError:
144-
LOGGER.error("Unable to get response from Comicvine") # noqa: TRY400
142+
LOGGER.error("Unable to get response from Comicvine")
145143
return None
144+
if search.comicvine:
145+
search.comicvine = None
146+
return self.fetch_series(series_id=series_id, search=search)
147+
return None
146148

147149
def _process_metron_info(self, series: Volume, issue: Issue) -> MetronInfo | None:
148150
from perdoo.metadata.metron_info import (

perdoo/services/marvel.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from esak.session import Session as Esak
1010
from esak.sqlite_cache import SqliteCache
1111
from natsort import humansorted, ns
12+
from requests.exceptions import ConnectionError, HTTPError # noqa: A004
1213
from rich.prompt import Confirm, Prompt
1314

1415
from perdoo import get_cache_root
@@ -59,10 +60,13 @@ def _search_series(self, name: str | None, volume: int | None, year: int | None)
5960
return self._search_series(name=name, volume=volume, year=None)
6061
if Confirm.ask("Search Again", console=CONSOLE):
6162
return self._search_series(name=None, volume=None, year=None)
62-
return None
63-
except ApiError:
64-
LOGGER.exception("")
65-
return None
63+
except ConnectionError as err:
64+
LOGGER.error(err)
65+
except HTTPError as err:
66+
LOGGER.error(err)
67+
except ApiError as err:
68+
LOGGER.error(err)
69+
return None
6670

6771
def fetch_series(self, search: SeriesSearch) -> Series | None:
6872
series_id = search.marvel or self._search_series(
@@ -74,9 +78,18 @@ def fetch_series(self, search: SeriesSearch) -> Series | None:
7478
series = self.session.series(_id=series_id)
7579
search.marvel = series_id
7680
return series
77-
except ApiError:
78-
LOGGER.exception("")
81+
except ConnectionError as err:
82+
LOGGER.error(err)
83+
return None
84+
except HTTPError as err:
85+
LOGGER.error(err)
7986
return None
87+
except ApiError as err:
88+
LOGGER.error(err)
89+
if search.marvel:
90+
search.marvel = None
91+
return self.fetch_series(search=search)
92+
return None
8093

8194
def _search_issue(self, series_id: int, number: str | None) -> int | None:
8295
try:
@@ -108,10 +121,13 @@ def _search_issue(self, series_id: int, number: str | None) -> int | None:
108121
if number:
109122
LOGGER.info("Searching again without the IssueNumber")
110123
return self._search_issue(series_id=series_id, number=None)
111-
return None
112-
except ApiError:
113-
LOGGER.exception("")
114-
return None
124+
except ConnectionError as err:
125+
LOGGER.error(err)
126+
except HTTPError as err:
127+
LOGGER.error(err)
128+
except ApiError as err:
129+
LOGGER.error(err)
130+
return None
115131

116132
def fetch_issue(self, series_id: int, search: IssueSearch) -> Comic | None:
117133
issue_id = search.marvel or self._search_issue(series_id=series_id, number=search.number)
@@ -121,9 +137,18 @@ def fetch_issue(self, series_id: int, search: IssueSearch) -> Comic | None:
121137
issue = self.session.comic(_id=issue_id)
122138
search.marvel = issue_id
123139
return issue
124-
except ApiError:
125-
LOGGER.exception("")
140+
except ConnectionError as err:
141+
LOGGER.error(err)
142+
return None
143+
except HTTPError as err:
144+
LOGGER.error(err)
126145
return None
146+
except ApiError as err:
147+
LOGGER.error(err)
148+
if search.marvel:
149+
search.marvel = None
150+
return self.fetch_issue(series_id=series_id, search=search)
151+
return None
127152

128153
def _process_metron_info(self, series: Series, issue: Comic) -> MetronInfo | None:
129154
from perdoo.metadata.metron_info import (

perdoo/services/metron.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ def _search_series_by_comicvine(self, comicvine_id: int | None) -> int | None:
3434
series = self.session.series_list({"cv_id": comicvine_id})
3535
if series and len(series) >= 1:
3636
return series[0].id
37-
return None
38-
except ApiError:
39-
LOGGER.exception("")
40-
return None
37+
except ApiError as err:
38+
LOGGER.error(err)
39+
return None
4140

4241
def _search_series(self, name: str | None, volume: int | None, year: int | None) -> int | None:
4342
name = name or Prompt.ask("Series Name", console=CONSOLE)
@@ -83,10 +82,9 @@ def _search_series(self, name: str | None, volume: int | None, year: int | None)
8382
return self._search_series(name=name, volume=None, year=None)
8483
if Confirm.ask("Search Again", console=CONSOLE):
8584
return self._search_series(name=None, volume=None, year=None)
86-
return None
87-
except ApiError:
88-
LOGGER.exception("")
89-
return None
85+
except ApiError as err:
86+
LOGGER.error(err)
87+
return None
9088

9189
def fetch_series(self, search: SeriesSearch) -> Series | None:
9290
series_id = (
@@ -100,9 +98,12 @@ def fetch_series(self, search: SeriesSearch) -> Series | None:
10098
series = self.session.series(_id=series_id)
10199
search.metron = series_id
102100
return series
103-
except ApiError:
104-
LOGGER.exception("")
105-
return None
101+
except ApiError as err:
102+
LOGGER.error(err)
103+
if search.metron:
104+
search.metron = None
105+
return self.fetch_series(search=search)
106+
return None
106107

107108
def _search_issue_by_comicvine(self, comicvine_id: int | None) -> int | None:
108109
if not comicvine_id:
@@ -111,10 +112,9 @@ def _search_issue_by_comicvine(self, comicvine_id: int | None) -> int | None:
111112
issues = self.session.issues_list({"cv_id": comicvine_id})
112113
if issues and len(issues) >= 1:
113114
return issues[0].id
114-
return None
115-
except ApiError:
116-
LOGGER.exception("")
117-
return None
115+
except ApiError as err:
116+
LOGGER.error(err)
117+
return None
118118

119119
def _search_issue(self, series_id: int, number: str | None) -> int | None:
120120
try:
@@ -144,10 +144,9 @@ def _search_issue(self, series_id: int, number: str | None) -> int | None:
144144
if number:
145145
LOGGER.info("Searching again without the Number")
146146
return self._search_issue(series_id=series_id, number=None)
147-
return None
148-
except ApiError:
149-
LOGGER.exception("")
150-
return None
147+
except ApiError as err:
148+
LOGGER.error(err)
149+
return None
151150

152151
def fetch_issue(self, series_id: int, search: IssueSearch) -> Issue | None:
153152
issue_id = (
@@ -161,9 +160,12 @@ def fetch_issue(self, series_id: int, search: IssueSearch) -> Issue | None:
161160
issue = self.session.issue(_id=issue_id)
162161
search.metron = issue_id
163162
return issue
164-
except ApiError:
165-
LOGGER.exception("")
166-
return None
163+
except ApiError as err:
164+
LOGGER.error(err)
165+
if search.metron:
166+
search.metron = None
167+
return self.fetch_issue(series_id=series_id, search=search)
168+
return None
167169

168170
def _process_metron_info(self, series: Series, issue: Issue) -> MetronInfo | None:
169171
from perdoo.metadata.metron_info import (

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ ignore = [
113113
"PLW2901",
114114
"TCH",
115115
"TRY003",
116-
"TRY300"
116+
"TRY300",
117+
"TRY400"
117118
]
118119
select = ["ALL"]
119120

0 commit comments

Comments
 (0)