-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
52 lines (40 loc) · 1.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
This program is free software: you can redistribute it under the terms
of the GNU General Public License, v. 3.0. If a copy of the GNU General
Public License was not distributed with this file, see <https://www.gnu.org/licenses/>.
"""
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from api_v1 import router as api_v1_router
from logutils import get_logger
logger = get_logger(__name__)
app = FastAPI(
title="RelaySMS Telemetry API",
description=(
"Collects, analyzes, and exposes RelaySMS "
"usage data for transparent telemetry insights."
),
redoc_url=None,
swagger_ui_parameters={"defaultModelsExpandDepth": -1},
)
@app.exception_handler(HTTPException)
def http_exception_handler(_, exc: HTTPException):
logger.error(exc.detail)
return JSONResponse(exc.detail, status_code=exc.status_code)
@app.exception_handler(RequestValidationError)
def validation_exception_handler(_, exc: RequestValidationError):
first_error = exc.errors()[0]
field = " ".join(str(loc) for loc in first_error["loc"])
message = first_error.get("msg", "Invalid input")
error_message = f"{field}, {message}"
logger.error(error_message)
return JSONResponse({"error": error_message}, status_code=400)
@app.exception_handler(Exception)
def internal_exception_handler(_, exc: Exception):
logger.exception(exc)
return JSONResponse(
{"error": "Oops! Something went wrong. Please try again later."},
status_code=500,
)
app.include_router(api_v1_router)