|
5 | 5 |
|
6 | 6 | from app.auth import oauth2_scheme |
7 | 7 | from app.database.db import get_db |
| 8 | +from app.error import ( |
| 9 | + AuthException, |
| 10 | + DispatcherException, |
| 11 | + ErrorResponse, |
| 12 | + InternalException, |
| 13 | + JobNotFoundException, |
| 14 | +) |
| 15 | +from app.middleware.error_handling import get_dispatcher_error_response |
8 | 16 | from app.schemas.enum import OutputFormatEnum, ProcessTypeEnum |
9 | 17 | from app.schemas.unit_job import ( |
10 | 18 | BaseJobRequest, |
|
30 | 38 | status_code=status.HTTP_201_CREATED, |
31 | 39 | tags=["Unit Jobs"], |
32 | 40 | summary="Create a new processing job", |
| 41 | + responses={ |
| 42 | + InternalException.http_status: { |
| 43 | + "description": "Internal server error", |
| 44 | + "model": ErrorResponse, |
| 45 | + "content": { |
| 46 | + "application/json": { |
| 47 | + "example": get_dispatcher_error_response( |
| 48 | + InternalException(), "request-id" |
| 49 | + ) |
| 50 | + } |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
33 | 54 | ) |
34 | 55 | async def create_unit_job( |
35 | 56 | payload: Annotated[ |
@@ -105,65 +126,100 @@ async def create_unit_job( |
105 | 126 | """Create a new processing job with the provided data.""" |
106 | 127 | try: |
107 | 128 | return await create_processing_job(token, db, payload) |
108 | | - except HTTPException as e: |
109 | | - raise e |
| 129 | + except DispatcherException as de: |
| 130 | + raise de |
110 | 131 | except Exception as e: |
111 | | - logger.exception(f"Error creating processing job: {e}") |
112 | | - raise HTTPException( |
113 | | - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
114 | | - detail=f"An error occurred while creating the processing job: {e}", |
| 132 | + logger.error(f"Error creating processing job: {e}") |
| 133 | + raise InternalException( |
| 134 | + message="An error occurred while creating processing job." |
115 | 135 | ) |
116 | 136 |
|
117 | 137 |
|
118 | 138 | @router.get( |
119 | 139 | "/unit_jobs/{job_id}", |
120 | 140 | tags=["Unit Jobs"], |
121 | | - responses={404: {"description": "Processing job not found"}}, |
| 141 | + responses={ |
| 142 | + JobNotFoundException.http_status: { |
| 143 | + "description": "Job not found", |
| 144 | + "model": ErrorResponse, |
| 145 | + "content": { |
| 146 | + "application/json": { |
| 147 | + "example": get_dispatcher_error_response( |
| 148 | + JobNotFoundException(), "request-id" |
| 149 | + ) |
| 150 | + } |
| 151 | + }, |
| 152 | + }, |
| 153 | + InternalException.http_status: { |
| 154 | + "description": "Internal server error", |
| 155 | + "model": ErrorResponse, |
| 156 | + "content": { |
| 157 | + "application/json": { |
| 158 | + "example": get_dispatcher_error_response( |
| 159 | + InternalException(), "request-id" |
| 160 | + ) |
| 161 | + } |
| 162 | + }, |
| 163 | + }, |
| 164 | + }, |
122 | 165 | ) |
123 | 166 | async def get_job( |
124 | 167 | job_id: int, db: Session = Depends(get_db), token: str = Depends(oauth2_scheme) |
125 | 168 | ) -> ProcessingJob: |
126 | 169 | try: |
127 | 170 | job = await get_processing_job_by_user_id(token, db, job_id) |
128 | 171 | if not job: |
129 | | - logger.error(f"Processing job {job_id} not found") |
130 | | - raise HTTPException( |
131 | | - status_code=404, |
132 | | - detail=f"Processing job {job_id} not found", |
133 | | - ) |
| 172 | + raise JobNotFoundException() |
134 | 173 | return job |
135 | | - except HTTPException as e: |
136 | | - raise e |
| 174 | + except DispatcherException as de: |
| 175 | + raise de |
137 | 176 | except Exception as e: |
138 | | - logger.exception(f"Error retrieving processing job {job_id}: {e}") |
139 | | - raise HTTPException( |
140 | | - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
141 | | - detail=f"An error occurred while retrieving processing job {job_id}: {e}", |
| 177 | + logger.error(f"Error retrieving processing job {job_id}: {e}") |
| 178 | + raise InternalException( |
| 179 | + message="An error occurred while retrieving the processing job." |
142 | 180 | ) |
143 | 181 |
|
144 | 182 |
|
145 | 183 | @router.get( |
146 | 184 | "/unit_jobs/{job_id}/results", |
147 | 185 | tags=["Unit Jobs"], |
148 | | - responses={404: {"description": "Processing job not found"}}, |
| 186 | + responses={ |
| 187 | + JobNotFoundException.http_status: { |
| 188 | + "description": "Job not found", |
| 189 | + "model": ErrorResponse, |
| 190 | + "content": { |
| 191 | + "application/json": { |
| 192 | + "example": get_dispatcher_error_response( |
| 193 | + JobNotFoundException(), "request-id" |
| 194 | + ) |
| 195 | + } |
| 196 | + }, |
| 197 | + }, |
| 198 | + InternalException.http_status: { |
| 199 | + "description": "Internal server error", |
| 200 | + "model": ErrorResponse, |
| 201 | + "content": { |
| 202 | + "application/json": { |
| 203 | + "example": get_dispatcher_error_response( |
| 204 | + InternalException(), "request-id" |
| 205 | + ) |
| 206 | + } |
| 207 | + }, |
| 208 | + }, |
| 209 | + }, |
149 | 210 | ) |
150 | 211 | async def get_job_results( |
151 | 212 | job_id: int, db: Session = Depends(get_db), token: str = Depends(oauth2_scheme) |
152 | 213 | ) -> Collection | None: |
153 | 214 | try: |
154 | 215 | result = await get_processing_job_results(token, db, job_id) |
155 | 216 | if not result: |
156 | | - logger.error(f"Result for processing job {job_id} not found") |
157 | | - raise HTTPException( |
158 | | - status_code=404, |
159 | | - detail=f"Result for processing job {job_id} not found", |
160 | | - ) |
| 217 | + raise JobNotFoundException() |
161 | 218 | return result |
162 | | - except HTTPException as e: |
163 | | - raise e |
| 219 | + except DispatcherException as de: |
| 220 | + raise de |
164 | 221 | except Exception as e: |
165 | | - logger.exception(f"Error getting results for processing job {job_id}: {e}") |
166 | | - raise HTTPException( |
167 | | - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, |
168 | | - detail=f"An error occurred while retrieving results for processing job {job_id}: {e}", |
| 222 | + logger.error(f"Error getting results for processing job {job_id}: {e}") |
| 223 | + raise InternalException( |
| 224 | + message="An error occurred while retrieving processing job results." |
169 | 225 | ) |
0 commit comments