|
| 1 | +import starlette.requests |
| 2 | +from starlette.responses import JSONResponse |
| 3 | + |
| 4 | +from print_service.base import StatusResponseModel |
| 5 | +from print_service.exceptions import ( |
| 6 | + AlreadyUploaded, |
| 7 | + FileIsNotReceived, |
| 8 | + FileNotFound, |
| 9 | + InvalidPageRequest, |
| 10 | + InvalidType, |
| 11 | + IsCorrupted, |
| 12 | + IsNotUploaded, |
| 13 | + NotInUnion, |
| 14 | + PINGenerateError, |
| 15 | + PINNotFound, |
| 16 | + TerminalQRNotFound, |
| 17 | + TerminalTokenNotFound, |
| 18 | + TooLargeSize, |
| 19 | + TooManyPages, |
| 20 | + UnionStudentDuplicate, |
| 21 | + UnprocessableFileInstance, |
| 22 | + UserNotFound, |
| 23 | +) |
| 24 | +from print_service.routes.base import app |
| 25 | + |
| 26 | + |
| 27 | +@app.exception_handler(TooLargeSize) |
| 28 | +async def too_large_size(req: starlette.requests.Request, exc: TooLargeSize): |
| 29 | + return JSONResponse( |
| 30 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=413 |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +@app.exception_handler(TooManyPages) |
| 35 | +async def too_many_pages(req: starlette.requests.Request, exc: TooManyPages): |
| 36 | + return JSONResponse( |
| 37 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=413 |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@app.exception_handler(InvalidPageRequest) |
| 42 | +async def invalid_format(req: starlette.requests.Request, exc: TooManyPages): |
| 43 | + return JSONResponse( |
| 44 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=416 |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +@app.exception_handler(TerminalQRNotFound) |
| 49 | +async def terminal_not_found_by_qr(req: starlette.requests.Request, exc: TerminalQRNotFound): |
| 50 | + return JSONResponse( |
| 51 | + content=StatusResponseModel(status="Error", message=f"Terminal not found by QR").dict(), |
| 52 | + status_code=400, |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +@app.exception_handler(TerminalTokenNotFound) |
| 57 | +async def terminal_not_found_by_token(req: starlette.requests.Request, exc: TerminalTokenNotFound): |
| 58 | + return JSONResponse( |
| 59 | + content=StatusResponseModel(status="Error", message=f"Terminal not found by token").dict(), |
| 60 | + status_code=400, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +@app.exception_handler(UserNotFound) |
| 65 | +async def user_not_found(req: starlette.requests.Request, exc: UserNotFound): |
| 66 | + return JSONResponse( |
| 67 | + content=StatusResponseModel(status="Error", message=f"User not found").dict(), status_code=404 |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@app.exception_handler(UnionStudentDuplicate) |
| 72 | +async def student_duplicate(req: starlette.requests.Request, exc: UnionStudentDuplicate): |
| 73 | + return JSONResponse( |
| 74 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=400 |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@app.exception_handler(NotInUnion) |
| 79 | +async def not_in_union(req: starlette.requests.Request, exc: NotInUnion): |
| 80 | + return JSONResponse( |
| 81 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=403 |
| 82 | + ) |
| 83 | + |
| 84 | + |
| 85 | +@app.exception_handler(PINGenerateError) |
| 86 | +async def generate_error(req: starlette.requests.Request, exc: PINGenerateError): |
| 87 | + return JSONResponse( |
| 88 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=500 |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +@app.exception_handler(FileIsNotReceived) |
| 93 | +async def file_not_received(req: starlette.requests.Request, exc: FileIsNotReceived): |
| 94 | + return JSONResponse( |
| 95 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=400 |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +@app.exception_handler(PINNotFound) |
| 100 | +async def pin_not_found(req: starlette.requests.Request, exc: PINNotFound): |
| 101 | + return JSONResponse( |
| 102 | + content=StatusResponseModel(status="Error", message=f"Pin {exc.pin} not found").dict(), |
| 103 | + status_code=404, |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +@app.exception_handler(InvalidType) |
| 108 | +async def invalid_type(req: starlette.requests.Request, exc: InvalidType): |
| 109 | + return JSONResponse( |
| 110 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=415 |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +@app.exception_handler(AlreadyUploaded) |
| 115 | +async def already_upload(req: starlette.requests.Request, exc: AlreadyUploaded): |
| 116 | + return JSONResponse( |
| 117 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=415 |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +@app.exception_handler(IsCorrupted) |
| 122 | +async def is_corrupted(req: starlette.requests.Request, exc: IsCorrupted): |
| 123 | + return JSONResponse( |
| 124 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=415 |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +@app.exception_handler(UnprocessableFileInstance) |
| 129 | +async def unprocessable_file_instance(req: starlette.requests.Request, exc: UnprocessableFileInstance): |
| 130 | + return JSONResponse( |
| 131 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=422 |
| 132 | + ) |
| 133 | + |
| 134 | + |
| 135 | +@app.exception_handler(FileNotFound) |
| 136 | +async def file_not_found(req: starlette.requests.Request, exc: FileNotFound): |
| 137 | + return JSONResponse( |
| 138 | + content=StatusResponseModel(status="Error", message=f"{exc.count} file(s) not found").dict(), |
| 139 | + status_code=404, |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +@app.exception_handler(IsNotUploaded) |
| 144 | +async def not_uploaded(req: starlette.requests.Request, exc: IsNotUploaded): |
| 145 | + return JSONResponse( |
| 146 | + content=StatusResponseModel(status="Error", message=f"{exc}").dict(), status_code=415 |
| 147 | + ) |
0 commit comments