Skip to content

Commit

Permalink
fix: Avoid undefined variable when the body is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafiot committed Feb 25, 2024
1 parent 18f2f71 commit 5a21965
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
19 changes: 8 additions & 11 deletions playwrightcapture/capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,19 +484,16 @@ async def store_request(request: Request) -> None:
if response := await request.response():
if response.ok:
try:
body = await response.body()
if body := await response.body():
try:
mimetype = from_string(body, mime=True)
if mimetype.startswith('image'):
self._requests[request.url] = body
except PureError:
# unable to identify the mimetype
self.logger.debug(f'Unable to identify the mimetype for {request.url}')
except Exception as e:
self.logger.debug(f'Unable to get body for {request.url}: {e}')
else:
try:
if body:
mimetype = from_string(body, mime=True)
except PureError:
# unable to identify the mimetype
self.logger.debug(f'Unable to identify the mimetype for {request.url}')
else:
if mimetype.startswith('image'):
self._requests[request.url] = body
except Exception as e:
self.logger.warning(f'Unable to store request: {e}')

Expand Down
14 changes: 7 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pytz = {"version" = "^2024.1", python = "<3.9"}
tzdata = "^2024.1"
playwright-stealth = "^1.0.6"
setuptools = "^69.1.1"
puremagic = "^1.20"
puremagic = "^1.21"

[tool.poetry.extras]
recaptcha = ["requests", "pydub", "SpeechRecognition"]
Expand All @@ -40,7 +40,7 @@ optional = true

[tool.poetry.group.dev.dependencies]
types-beautifulsoup4 = "^4.12.0.20240106"
pytest = "^8.0.1"
pytest = "^8.0.2"
mypy = "^1.8.0"
types-dateparser = "^1.1.4.20240106"
types-requests = "^2.31.0.20240218"
Expand Down

0 comments on commit 5a21965

Please sign in to comment.