Skip to content

Invalid env causes all requests to fail #173

@alexbeattie42

Description

@alexbeattie42

Having an invalid env with apitally middleware causes all fastapi requests to fail.
Related: #160

Reproduction

Using the following value will reproduce the issue:

app.add_middleware(
        ApitallyMiddleware,
        client_id=settings.apitally_client_id,
        env=1234,
    )

Issue

Setting the env to a type other than str type causes the following error (also None).

  File ".venv/lib/python3.13/site-packages/apitally/client/client_base.py", line 56, in __init__
    if re.match(r"^[\w-]{1,32}$", env) is None:
       ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.13/re/__init__.py", line 167, in match
    return _compile(pattern, flags).match(string)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
TypeError: expected string or bytes-like object, got 'int'

Mypy will complain about doing this, but there isn't anything preventing the consumer from trying.
The re.match is expecting a str type and when it gets int it throws an exception.

Solution

An invalid env should be logged as an error, but not cause a 500 error which prevents any requests from being processed.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions