Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BACKEND_CORS_ORIGINS json decode error #35

Open
ChrisB1123 opened this issue May 25, 2022 · 1 comment
Open

BACKEND_CORS_ORIGINS json decode error #35

ChrisB1123 opened this issue May 25, 2022 · 1 comment

Comments

@ChrisB1123
Copy link

ChrisB1123 commented May 25, 2022

Hi, when trying to push part 13 to Heroku, I kept getting the below error messages, which looks like an issue parsing BACKEND_CORS_ORIGINS as a json list.

I've managed to solve the issue by adding in a few json.dumps() when declaring/validating the variable - as seen below.

I'm not sure why the app was crashing on Heroku, because it was working fine locally, and I'm not yet sure if this will have any knock on effects anywhere else, but just wanted to share, to hopefully save someone else some time in the future!

Thanks for the tutorial @ChristopherGS!!

New code in config.py file:

BACKEND_CORS_ORIGINS = json.dumps([
        "http://localhost:3000",
        "http://localhost:8001"
    ])

   ....

    @validator("BACKEND_CORS_ORIGINS", pre=True)  # 3
    def assemble_cors_origins(cls, v: Union[str, List[str]]) -> Union[List[str], str]:
        if isinstance(v, str) and not v.startswith("["):
            return json.dumps([i.strip() for i in v.split(",")])
        elif isinstance(v, (list, str)):
            return json.dumps(v)
        raise ValueError(v)

error message I was getting:

2022-05-25T21:39:26.798355+00:00 heroku[web.1]: State changed from starting to up
2022-05-25T21:39:27.015783+00:00 app[web.1]: [2022-05-25 21:39:27 +0000] [8] [ERROR] Exception in worker process
2022-05-25T21:39:27.015790+00:00 app[web.1]: Traceback (most recent call last):
2022-05-25T21:39:27.015790+00:00 app[web.1]:   File "pydantic/env_settings.py", line 172, in pydantic.env_settings.EnvSettingsSource.__call__
2022-05-25T21:39:27.015791+00:00 app[web.1]:   File "/usr/local/lib/python3.9/json/__init__.py", line 346, in loads
2022-05-25T21:39:27.015791+00:00 app[web.1]:     return _default_decoder.decode(s)
2022-05-25T21:39:27.015791+00:00 app[web.1]:   File "/usr/local/lib/python3.9/json/decoder.py", line 337, in decode
2022-05-25T21:39:27.015792+00:00 app[web.1]:     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
2022-05-25T21:39:27.015792+00:00 app[web.1]:   File "/usr/local/lib/python3.9/json/decoder.py", line 355, in raw_decode
2022-05-25T21:39:27.015792+00:00 app[web.1]:     raise JSONDecodeError("Expecting value", s, err.value) from None
2022-05-25T21:39:27.015793+00:00 app[web.1]: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
2022-05-25T21:39:27.015793+00:00 app[web.1]: 
2022-05-25T21:39:27.015794+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2022-05-25T21:39:27.015794+00:00 app[web.1]: 
2022-05-25T21:39:27.015794+00:00 app[web.1]: Traceback (most recent call last):
2022-05-25T21:39:27.015795+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2022-05-25T21:39:27.015795+00:00 app[web.1]:     worker.init_process()
2022-05-25T21:39:27.015795+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/uvicorn/workers.py", line 61, in init_process
2022-05-25T21:39:27.015795+00:00 app[web.1]:     super(UvicornWorker, self).init_process()
2022-05-25T21:39:27.015795+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
2022-05-25T21:39:27.015796+00:00 app[web.1]:     self.load_wsgi()
2022-05-25T21:39:27.015796+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2022-05-25T21:39:27.015796+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2022-05-25T21:39:27.015796+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
2022-05-25T21:39:27.015797+00:00 app[web.1]:     self.callable = self.load()
2022-05-25T21:39:27.015797+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2022-05-25T21:39:27.015797+00:00 app[web.1]:     return self.load_wsgiapp()
2022-05-25T21:39:27.015797+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2022-05-25T21:39:27.015797+00:00 app[web.1]:     return util.import_app(self.app_uri)
2022-05-25T21:39:27.015798+00:00 app[web.1]:   File "/usr/local/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
2022-05-25T21:39:27.015798+00:00 app[web.1]:     mod = importlib.import_module(module)
2022-05-25T21:39:27.015798+00:00 app[web.1]:   File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module
2022-05-25T21:39:27.015798+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2022-05-25T21:39:27.015798+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2022-05-25T21:39:27.015799+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2022-05-25T21:39:27.015799+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
2022-05-25T21:39:27.015799+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
2022-05-25T21:39:27.015799+00:00 app[web.1]:   File "<frozen importlib._bootstrap_external>", line 850, in exec_module
2022-05-25T21:39:27.015799+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
2022-05-25T21:39:27.015799+00:00 app[web.1]:   File "/app/app/main.py", line 10, in <module>
2022-05-25T21:39:27.015799+00:00 app[web.1]:     from app.api import deps
2022-05-25T21:39:27.015800+00:00 app[web.1]:   File "/app/app/api/deps.py", line 8, in <module>
2022-05-25T21:39:27.015800+00:00 app[web.1]:     from app.core.auth import oauth2_scheme
2022-05-25T21:39:27.015800+00:00 app[web.1]:   File "/app/app/core/auth.py", line 9, in <module>
2022-05-25T21:39:27.015800+00:00 app[web.1]:     from app.core.config import settings
2022-05-25T21:39:27.015800+00:00 app[web.1]:   File "/app/app/core/config.py", line 52, in <module>
2022-05-25T21:39:27.015801+00:00 app[web.1]:     settings = Settings()
2022-05-25T21:39:27.015801+00:00 app[web.1]:   File "pydantic/env_settings.py", line 37, in pydantic.env_settings.BaseSettings.__init__
2022-05-25T21:39:27.015801+00:00 app[web.1]:   File "pydantic/env_settings.py", line 63, in pydantic.env_settings.BaseSettings._build_values
2022-05-25T21:39:27.015801+00:00 app[web.1]:   File "pydantic/env_settings.py", line 174, in pydantic.env_settings.EnvSettingsSource.__call__
2022-05-25T21:39:27.015801+00:00 app[web.1]: pydantic.env_settings.SettingsError: error parsing JSON for "BACKEND_CORS_ORIGINS"
2022-05-25T21:39:27.015961+00:00 app[web.1]: [2022-05-25 21:39:27 +0000] [8] [INFO] Worker exiting (pid: 8)
2022-05-25T21:39:27.110253+00:00 app[web.1]: [2022-05-25 21:39:27 +0000] [4] [INFO] Shutting down: Master
2022-05-25T21:39:27.110284+00:00 app[web.1]: [2022-05-25 21:39:27 +0000] [4] [INFO] Reason: Worker failed to boot.
2022-05-25T21:39:27.284599+00:00 heroku[web.1]: Process exited with status 3
2022-05-25T21:39:27.563745+00:00 heroku[web.1]: State changed from up to crashed
@ChrisB1123
Copy link
Author

Just to add to this, I believe the error is caused by the bug which is highlighted in the below issue
pydantic/pydantic#1458

And will hopefully be fixed once this fix is released
pydantic/pydantic#3977

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant