Applying the exception handlers
The exception handler does a redirection to the appropriate microservice. As you learned in Chapter 2, Exploring the Core Features, each thrown exception must have its corresponding exception handler to pursue the required response after the exception handling. Here are the exception handlers that will handle the custom exception thrown by call_api_gateway()
:
from fastapi.responses import RedirectResponse from gateway.api_router import call_api_gateway, RedirectStudentPortalException, RedirectFacultyPortalException, RedirectLibraryPortalException @app.exception_handler(RedirectStudentPortalException) def exception_handler_student(request: Request, exc: RedirectStudentPortalException) -> Response: return RedirectResponse( url='http://localhost:8000/ch04/student/index') @app...