-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from Qwizi/email
Updated email service and added better error exception logging
- Loading branch information
Showing
12 changed files
with
215 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"""Exception handlers for FastAPI.""" | ||
from __future__ import annotations | ||
|
||
import sys | ||
from typing import Union | ||
|
||
from fastapi import Request | ||
from fastapi.exception_handlers import http_exception_handler as _http_exception_handler | ||
from fastapi.exception_handlers import ( | ||
request_validation_exception_handler as _request_validation_exception_handler, | ||
) | ||
from fastapi.exceptions import HTTPException, RequestValidationError | ||
from fastapi.responses import JSONResponse, PlainTextResponse, Response | ||
|
||
from sharkservers.logger import logger | ||
|
||
|
||
async def request_validation_exception_handler( | ||
request: Request, exc: RequestValidationError | ||
) -> JSONResponse: | ||
""" | ||
Middleware will log all RequestValidationErrors. | ||
Args: | ||
---- | ||
request: The request object. | ||
exc: The RequestValidationError exception. | ||
Returns: | ||
------- | ||
A JSONResponse with the errors and the request body. | ||
""" | ||
logger.debug("Our custom request_validation_exception_handler was called") | ||
body = await request.body() | ||
query_params = request.query_params._dict # pylint: disable=protected-access | ||
detail = { | ||
"errors": exc.errors(), | ||
"body": body.decode(), | ||
"query_params": query_params, | ||
} | ||
logger.info(detail) | ||
return await _request_validation_exception_handler(request, exc) | ||
|
||
|
||
async def http_exception_handler( | ||
request: Request, exc: HTTPException | ||
) -> Union[JSONResponse, Response]: | ||
""" | ||
HTTPException handler. | ||
Args: | ||
---- | ||
request: The request object. | ||
exc: The HTTPException exception. | ||
Returns: | ||
------- | ||
A JSONResponse with the error. | ||
""" | ||
logger.error(f"{exc.detail} <{exc.detail.value}>") | ||
return await _http_exception_handler(request, exc) | ||
|
||
|
||
async def unhandled_exception_handler( | ||
request: Request, exc: Exception | ||
) -> PlainTextResponse: | ||
""" | ||
Unhandled exception handler. | ||
Args: | ||
---- | ||
request: The request object. | ||
exc: The exception. | ||
Returns: | ||
------- | ||
A PlainTextResponse with the error. | ||
""" | ||
logger.debug("Our custom unhandled_exception_handler was called") | ||
host = getattr(getattr(request, "client", None), "host", None) | ||
port = getattr(getattr(request, "client", None), "port", None) | ||
url = ( | ||
f"{request.url.path}?{request.query_params}" | ||
if request.query_params | ||
else request.url.path | ||
) | ||
exception_type, exception_value, exception_traceback = sys.exc_info() | ||
exception_name = getattr(exception_type, "__name__", None) | ||
logger.error( | ||
f'{host}:{port} - "{request.method} {url}" 500 Internal Server Error <{exception_name}: {exception_value}>', | ||
) | ||
return PlainTextResponse(str(exc), status_code=500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import http | ||
import time | ||
|
||
from fastapi import Request | ||
|
||
from sharkservers.logger import logger | ||
|
||
|
||
async def log_request_middleware(request: Request, call_next): | ||
""" | ||
This middleware will log all requests and their processing time. | ||
E.g. log: | ||
0.0.0.0:1234 - GET /ping 200 OK 1.00ms | ||
""" | ||
logger.debug("middleware: log_request_middleware") | ||
url = ( | ||
f"{request.url.path}?{request.query_params}" | ||
if request.query_params | ||
else request.url.path | ||
) | ||
start_time = time.time() | ||
response = await call_next(request) | ||
process_time = (time.time() - start_time) * 1000 | ||
formatted_process_time = f"{process_time:.2f}" | ||
host = getattr(getattr(request, "client", None), "host", None) | ||
port = getattr(getattr(request, "client", None), "port", None) | ||
try: | ||
status_phrase = http.HTTPStatus(response.status_code).phrase | ||
except ValueError: | ||
status_phrase = "" | ||
logger.info( | ||
f'{host}:{port} - "{request.method} {url}" {response.status_code} {status_phrase} {formatted_process_time}ms' | ||
) | ||
return response |
Oops, something went wrong.