forked from apache/airflow
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AIP-81 Add Insert Multiple Pools API (apache#44121)
* Add bulk post pools, refactor post pool * Add 409 case for TestPostPool * Add test for bulk post pools * Remove unused status code, rename post_body to body * Refactor duplicate pool insert handling - handle exception from db level instead of application level * Add global database exception handler for fastapi * Remove manual handle for unique constraint exc * Refactor test_pools * Fix bound for TypeVar, type for comment
- Loading branch information
1 parent
fc52d7d
commit f33166a
Showing
12 changed files
with
439 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Generic, TypeVar | ||
|
||
from fastapi import HTTPException, Request, status | ||
from sqlalchemy.exc import IntegrityError | ||
|
||
T = TypeVar("T", bound=Exception) | ||
|
||
|
||
class BaseErrorHandler(Generic[T], ABC): | ||
"""Base class for error handlers.""" | ||
|
||
def __init__(self, exception_cls: T) -> None: | ||
self.exception_cls = exception_cls | ||
|
||
@abstractmethod | ||
def exception_handler(self, request: Request, exc: T): | ||
"""exception_handler method.""" | ||
raise NotImplementedError | ||
|
||
|
||
class _UniqueConstraintErrorHandler(BaseErrorHandler[IntegrityError]): | ||
"""Exception raised when trying to insert a duplicate value in a unique column.""" | ||
|
||
def __init__(self): | ||
super().__init__(IntegrityError) | ||
self.unique_constraint_error_messages = [ | ||
"UNIQUE constraint failed", # SQLite | ||
"Duplicate entry", # MySQL | ||
"violates unique constraint", # PostgreSQL | ||
] | ||
|
||
def exception_handler(self, request: Request, exc: IntegrityError): | ||
"""Handle IntegrityError exception.""" | ||
exc_orig_str = str(exc.orig) | ||
if any(error_msg in exc_orig_str for error_msg in self.unique_constraint_error_messages): | ||
raise HTTPException( | ||
status_code=status.HTTP_409_CONFLICT, | ||
detail="Unique constraint violation", | ||
) | ||
|
||
|
||
DatabaseErrorHandlers = [ | ||
_UniqueConstraintErrorHandler(), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.