|
3 | 3 |
|
4 | 4 | import os |
5 | 5 | from contextlib import asynccontextmanager |
6 | | -from http import HTTPStatus |
7 | 6 |
|
8 | 7 | from fastapi import FastAPI |
9 | 8 | from fastapi.middleware.cors import CORSMiddleware |
|
17 | 16 | from starlette.middleware.sessions import SessionMiddleware |
18 | 17 | from uvicorn.main import run |
19 | 18 |
|
20 | | -from modules.crawler.crawler_exceptions import NoDataToCrawlError |
21 | | -from repos.elastic.elastic_crud_base import NoSuchObjectInElasticSearchError |
| 19 | +from common.exception_handler import exception_handlers |
22 | 20 | from repos.elastic.elastic_repo import ElasticSearchRepo |
23 | 21 | from repos.llm_repo import LLMRepo |
24 | 22 | from utils.import_utils import import_by_suffix |
|
40 | 38 | startup(reset_data=RESET_DATA, sql_echo=False) |
41 | 39 | os.environ["STARTUP_DONE"] = "1" |
42 | 40 |
|
| 41 | +from rq.exceptions import NoSuchJobError |
| 42 | + |
43 | 43 | from config import conf |
44 | | -from core.auth.authz_user import ForbiddenError |
45 | | -from core.auth.validation import InvalidError |
46 | | -from core.doc.source_document_crud import SourceDocumentPreprocessingUnfinishedError |
47 | | -from modules.eximport.export_service import ( |
48 | | - ExportJobPreparationError, |
49 | | - NoSuchExportJobError, |
50 | | -) |
51 | | -from modules.eximport.import_service import ImportJobPreparationError |
52 | | -from modules.eximport.no_data_export_error import NoDataToExportError |
53 | | -from repos.db.crud_base import NoSuchElementError |
54 | | -from repos.filesystem_repo import ( |
55 | | - FileAlreadyExistsInFilesystemError, |
56 | | - FileNotFoundInFilesystemError, |
57 | | - FilesystemRepo, |
58 | | - SourceDocumentNotFoundInFilesystemError, |
59 | | -) |
| 44 | +from repos.filesystem_repo import FilesystemRepo |
60 | 45 |
|
61 | 46 | # import all jobs dynamically |
62 | 47 | import_by_suffix("_job.py") |
@@ -126,100 +111,32 @@ def custom_openapi(): |
126 | 111 | app.add_middleware(SessionMiddleware, secret_key=conf.api.auth.session.secret) |
127 | 112 |
|
128 | 113 |
|
129 | | -# add custom exception handlers |
130 | | -# TODO Flo: find a better place for this! (and Exceptions in general. move into own file) |
131 | | -@app.exception_handler(NoSuchElementError) |
132 | | -async def no_such_element_error_handler(_, exc: NoSuchElementError): |
133 | | - return PlainTextResponse(str(exc), status_code=404) |
134 | | - |
135 | | - |
136 | | -@app.exception_handler(NoDataToCrawlError) |
137 | | -async def no_data_to_crawl_handler(_, exc: NoDataToCrawlError): |
138 | | - return PlainTextResponse(str(exc), status_code=400) |
139 | | - |
140 | | - |
141 | | -@app.exception_handler(NoDataToExportError) |
142 | | -async def no_data_to_export_handler(_, exc: NoDataToExportError): |
143 | | - return PlainTextResponse(str(exc), status_code=404) |
144 | | - |
145 | | - |
146 | | -@app.exception_handler(NoSuchExportJobError) |
147 | | -async def no_such_export_job_handler(_, exc: NoSuchExportJobError): |
148 | | - return PlainTextResponse(str(exc), status_code=404) |
149 | | - |
150 | | - |
151 | | -@app.exception_handler(ExportJobPreparationError) |
152 | | -async def export_job_preparation_error_handler(_, exc: ExportJobPreparationError): |
153 | | - return PlainTextResponse(str(exc), status_code=500) |
154 | | - |
155 | | - |
156 | | -@app.exception_handler(ImportJobPreparationError) |
157 | | -async def import_job_preparation_error_handler(_, exc: ImportJobPreparationError): |
158 | | - return PlainTextResponse(str(exc), status_code=500) |
159 | | - |
160 | | - |
161 | | -@app.exception_handler(NoSuchObjectInElasticSearchError) |
162 | | -async def no_such_object_in_es_error_handler(_, exc: NoSuchObjectInElasticSearchError): |
163 | | - return PlainTextResponse(str(exc), status_code=500) |
164 | | - |
165 | | - |
166 | | -@app.exception_handler(SourceDocumentNotFoundInFilesystemError) |
167 | | -async def source_document_not_found_in_filesystem_error_handler( |
168 | | - _, exc: SourceDocumentNotFoundInFilesystemError |
169 | | -): |
170 | | - return PlainTextResponse(str(exc), status_code=500) |
171 | | - |
172 | | - |
173 | | -@app.exception_handler(SourceDocumentPreprocessingUnfinishedError) |
174 | | -async def source_document_preprocessing_unfinished_error_handler( |
175 | | - _, exc: SourceDocumentPreprocessingUnfinishedError |
176 | | -): |
177 | | - return PlainTextResponse(str(exc), status_code=500) |
178 | | - |
179 | | - |
180 | | -@app.exception_handler(FileNotFoundInFilesystemError) |
181 | | -async def file_not_found_in_filesystem_error_handler( |
182 | | - _, exc: FileNotFoundInFilesystemError |
183 | | -): |
184 | | - return PlainTextResponse(str(exc), status_code=500) |
185 | | - |
186 | | - |
187 | | -@app.exception_handler(FileAlreadyExistsInFilesystemError) |
188 | | -async def file_already_exists_in_filesystem_error_handler( |
189 | | - _, exc: FileAlreadyExistsInFilesystemError |
190 | | -): |
191 | | - return PlainTextResponse(str(exc), status_code=406) |
| 114 | +# import & register all endpoints dynamically |
| 115 | +endpoint_modules = import_by_suffix("_endpoint.py") |
| 116 | +endpoint_modules.sort(key=lambda x: x.__name__.split(".")[-1]) |
| 117 | +for em in endpoint_modules: |
| 118 | + app.include_router(em.router) |
192 | 119 |
|
193 | 120 |
|
194 | 121 | @app.exception_handler(IntegrityError) |
195 | 122 | async def integrity_error_handler(_, exc: IntegrityError): |
| 123 | + logger.exception(exc) |
196 | 124 | if isinstance(exc.orig, UniqueViolation): |
197 | 125 | msg = str(exc.orig.pgerror).split("\n")[1] |
198 | 126 | return PlainTextResponse(msg, status_code=409) |
199 | 127 | else: |
200 | 128 | return PlainTextResponse(str(exc), status_code=500) |
201 | 129 |
|
202 | 130 |
|
203 | | -@app.exception_handler(ForbiddenError) |
204 | | -def forbidden_error_handler(_, exc: ForbiddenError): |
205 | | - return PlainTextResponse(str(exc), status_code=403) |
206 | | - |
207 | | - |
208 | | -@app.exception_handler(InvalidError) |
209 | | -def invalid_error_handler(_, exc: InvalidError): |
210 | | - return PlainTextResponse(str(exc), status_code=HTTPStatus.BAD_REQUEST) |
211 | | - |
| 131 | +@app.exception_handler(NoSuchJobError) |
| 132 | +async def no_such_job_error_handler(_, exc: NoSuchJobError): |
| 133 | + logger.exception(exc) |
| 134 | + return PlainTextResponse(str(exc), status_code=404) |
212 | 135 |
|
213 | | -# import & register all exception handlers dynamically |
214 | | -exception_hanlders = import_by_suffix("_exception_handler.py") |
215 | | -for eh in exception_hanlders: |
216 | | - eh.register_exception_handlers(app) |
217 | 136 |
|
218 | | -# import & register all endpoints dynamically |
219 | | -endpoint_modules = import_by_suffix("_endpoint.py") |
220 | | -endpoint_modules.sort(key=lambda x: x.__name__.split(".")[-1]) |
221 | | -for em in endpoint_modules: |
222 | | - app.include_router(em.router) |
| 137 | +# register all exception handlers in fastAPI |
| 138 | +for ex_class, handler_func in exception_handlers: |
| 139 | + app.add_exception_handler(ex_class, handler_func) |
223 | 140 |
|
224 | 141 |
|
225 | 142 | def main() -> None: |
|
0 commit comments