aboutcode-org / aboutcode-org/purldb

NameError: 'e' referenced outside except block in visit_uri() when visit_error is set

未关闭
#851 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
HTML
星标
67
派生
69
平均合并
8 天 8 小时
30 天内合并 PR
1

描述

**Description**

In `visit_uri()` within `minecode/management/commands/run_visit.py`, the exception variable `e` is referenced outside the `except` block where it is defined.

Inside the `try/except` block, the variable `e` is created only when an exception occurs:

```python
except (ConnectionError, Timeout, Exception) as e:
msg = f"Visit error for URI: {uri_to_visit}"
msg += "\n".format()
msg += get_error_message(e)
visit_errors.append(msg)
logger.error(msg)
```

Later in the function, `e` is used again inside the `if visit_error:` block:

```python
if visit_error:
msg = f"Visit error for URI: {uri_to_visit}"
msg += "\n".format()
msg += get_error_message(e) # NOQA
```

However, this block can execute even when no exception was raised. In such cases, the variable `e` is not defined, which results in a runtime error:

```
NameError: name 'e' is not defined
```

This happens when `_visit_router.process(uri_to_visit)` returns a `visit_error` value without raising an exception.

Additionally, the `# NOQA` comment suppresses lint warnings that would otherwise highlight this issue.

To fix this, the code should avoid referencing `e` outside the `except` block and instead use the `visit_error` value or store the exception in a variable defined before the `try` block.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。