Skip to content

fixed app imort failures not being logged #273

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

Merged
merged 6 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ jobs:
- { name: "3.9", python: "3.9", os: ubuntu-latest }
- { name: "3.8", python: "3.8", os: ubuntu-latest }
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/setup-node@v2
- uses: actions/setup-node@v4
with:
node-version: "14"
- run: npm install

- uses: actions/setup-python@v2
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python }}
- name: update pip
Expand All @@ -43,7 +43,7 @@ jobs:
- run: npm run pytest
- run: npm run pylint

- uses: codecov/codecov-action@v2
- uses: codecov/codecov-action@v4
with:
token: "89d22de7-bfaf-43a0-81da-33cc733fd294"
fail_ci_if_error: true
Expand Down
8 changes: 5 additions & 3 deletions wsgi_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import importlib
import io
import json
import logging
import os
import sys
import traceback
from werkzeug.exceptions import InternalServerError

# Call decompression helper from `serverless-python-requirements` if
# available. See: https://github.com/UnitedIncome/serverless-python-requirements#dealing-with-lambdas-size-limitations
Expand Down Expand Up @@ -43,9 +45,9 @@ def import_app(config):
wsgi_module = importlib.import_module(wsgi_fqn_parts[-1])

return getattr(wsgi_module, wsgi_fqn[1])
except: # noqa
traceback.print_exc()
raise Exception("Unable to import {}".format(config["app"]))
except Exception as err:
logging.exception("Unable to import app: '{}' - {}".format(config["app"], err))
return InternalServerError("Unable to import app: {}".format(config["app"]))


def append_text_mime_types(config):
Expand Down
16 changes: 11 additions & 5 deletions wsgi_handler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,11 +812,17 @@ def test_command_unknown(mock_wsgi_app_file, mock_app, wsgi_handler):
assert "Exception: Unknown command: unknown" in response[1]


def test_app_import_error(mock_wsgi_app_file, mock_app_with_import_error, event_v1):
with pytest.raises(Exception, match="Unable to import app.app"):
if "wsgi_handler" in sys.modules:
del sys.modules["wsgi_handler"]
import wsgi_handler # noqa: F401
def test_app_import_error(mock_wsgi_app_file, mock_app_with_import_error, event_v1, wsgi_handler):
response = wsgi_handler.handler(event_v1, {})
assert response == {
"statusCode": 500,
"body": "<!doctype html>\n<html lang=en>\n<title>500 Internal Server Error</title>\n<h1>Internal Server Error</h1>\n<p>Unable to import app: app.app</p>\n",
"headers": {
"Content-Type": "text/html; charset=utf-8",
"Content-Length": "140"
},
"isBase64Encoded": False
}


def test_handler_with_encoded_characters_in_path(
Expand Down
Loading