Skip to content

Commit

Permalink
unified error handling for model create/update form
Browse files Browse the repository at this point in the history
  • Loading branch information
dustins committed Oct 17, 2024
1 parent 2e024eb commit 84d8f11
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lambda/models/lambda_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import boto3
import botocore.session
from fastapi import FastAPI, Path, Request
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from mangum import Mangum
Expand Down Expand Up @@ -63,6 +65,14 @@ async def model_not_found_handler(request: Request, exc: ModelNotFoundError) ->
return JSONResponse(status_code=404, content={"message": str(exc)})


@app.exception_handler(RequestValidationError) # type: ignore
async def validation_exception_handler(request: Request, exc: RequestValidationError):
"""Handle exception when request fails validation and and translate to a 422 error."""
return JSONResponse(
status_code=422, content={"detail": jsonable_encoder(exc.errors()), "type": "RequestValidationError"}
)


@app.exception_handler(InvalidStateTransitionError) # type: ignore
@app.exception_handler(ModelAlreadyExistsError) # type: ignore
@app.exception_handler(ValueError) # type: ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const modelManagementApi = createApi({
// transform into SerializedError
return {
name: 'Create Model Error',
message: baseQueryReturnValue.data.detail.map((error) => error.msg).join(', ')
message: baseQueryReturnValue.data?.type === 'RequestValidationError' ? baseQueryReturnValue.data.detail.map((error) => error.msg).join(', ') : baseQueryReturnValue.data.message
};
},
invalidatesTags: ['models'],
Expand All @@ -61,7 +61,7 @@ export const modelManagementApi = createApi({
// transform into SerializedError
return {
name: 'Update Model Error',
message: baseQueryReturnValue.data.detail.map((error) => error.msg).join(', ')
message: baseQueryReturnValue.data?.type === 'RequestValidationError' ? baseQueryReturnValue.data.detail.map((error) => error.msg).join(', ') : baseQueryReturnValue.data.message
};
},
invalidatesTags: ['models'],
Expand Down

0 comments on commit 84d8f11

Please sign in to comment.