Skip to content

Commit

Permalink
wip: initial set of exceptions added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedantsahai18 committed Dec 20, 2024
1 parent 7b0be5c commit dc0ec36
Show file tree
Hide file tree
Showing 28 changed files with 283 additions and 167 deletions.
58 changes: 28 additions & 30 deletions agents-api/agents_api/queries/agents/create_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
from beartype import beartype
from sqlglot import parse_one
from uuid_extensions import uuid7

import asyncpg
from fastapi import HTTPException
from ...autogen.openapi_model import Agent, CreateAgentRequest
from ...metrics.counters import increase_counter
from ..utils import (
generate_canonical_name,
pg_query,
wrap_in_class,
rewrap_exceptions,
partialclass,
)

# Define the raw SQL query
Expand Down Expand Up @@ -45,35 +48,30 @@
""").sql(pretty=True)


# @rewrap_exceptions(
# {
# psycopg_errors.ForeignKeyViolation: partialclass(
# HTTPException,
# status_code=404,
# detail="The specified developer does not exist.",
# ),
# psycopg_errors.UniqueViolation: partialclass(
# HTTPException,
# status_code=409,
# detail="An agent with this canonical name already exists for this developer.",
# ),
# psycopg_errors.CheckViolation: partialclass(
# HTTPException,
# status_code=400,
# detail="The provided data violates one or more constraints. Please check the input values.",
# ),
# ValidationError: partialclass(
# HTTPException,
# status_code=400,
# detail="Input validation failed. Please check the provided data.",
# ),
# TypeError: partialclass(
# HTTPException,
# status_code=400,
# detail="A type mismatch occurred. Please review the input.",
# ),
# }
# )
@rewrap_exceptions(
{
asyncpg.exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
asyncpg.exceptions.UniqueViolationError: partialclass(
HTTPException,
status_code=409,
detail="An agent with this canonical name already exists for this developer.",
),
asyncpg.exceptions.CheckViolationError: partialclass(
HTTPException,
status_code=400,
detail="The provided data violates one or more constraints. Please check the input values.",
),
asyncpg.exceptions.DataError: partialclass(
HTTPException,
status_code=400,
detail="Invalid data provided. Please check the input values.",
),
}
)
@wrap_in_class(
Agent,
one=True,
Expand Down
37 changes: 28 additions & 9 deletions agents-api/agents_api/queries/agents/create_or_update_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

from beartype import beartype
from sqlglot import parse_one
from fastapi import HTTPException
import asyncpg

from ...autogen.openapi_model import Agent, CreateOrUpdateAgentRequest
from ...metrics.counters import increase_counter
from ..utils import (
generate_canonical_name,
pg_query,
wrap_in_class,
rewrap_exceptions,
partialclass,
)

# Define the raw SQL query
Expand Down Expand Up @@ -44,15 +48,30 @@
""").sql(pretty=True)


# @rewrap_exceptions(
# {
# psycopg_errors.ForeignKeyViolation: partialclass(
# HTTPException,
# status_code=404,
# detail="The specified developer does not exist.",
# )
# }
# )
@rewrap_exceptions(
{
asyncpg.exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
asyncpg.exceptions.UniqueViolationError: partialclass(
HTTPException,
status_code=409,
detail="An agent with this canonical name already exists for this developer.",
),
asyncpg.exceptions.CheckViolationError: partialclass(
HTTPException,
status_code=400,
detail="The provided data violates one or more constraints. Please check the input values.",
),
asyncpg.exceptions.DataError: partialclass(
HTTPException,
status_code=400,
detail="Invalid data provided. Please check the input values.",
),
}
)
@wrap_in_class(
Agent,
one=True,
Expand Down
39 changes: 28 additions & 11 deletions agents-api/agents_api/queries/agents/delete_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

from beartype import beartype
from sqlglot import parse_one
from fastapi import HTTPException
import asyncpg

from ...autogen.openapi_model import ResourceDeletedResponse
from ...common.utils.datetime import utcnow
from ..utils import (
pg_query,
wrap_in_class,
rewrap_exceptions,
partialclass,
)

# Define the raw SQL query
Expand Down Expand Up @@ -59,17 +63,30 @@
""").sql(pretty=True)


# @rewrap_exceptions(
# @rewrap_exceptions(
# {
# psycopg_errors.ForeignKeyViolation: partialclass(
# HTTPException,
# status_code=404,
# detail="The specified developer does not exist.",
# )
# }
# # TODO: Add more exceptions
# )
@rewrap_exceptions(
{
asyncpg.exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
asyncpg.exceptions.UniqueViolationError: partialclass(
HTTPException,
status_code=409,
detail="An agent with this canonical name already exists for this developer.",
),
asyncpg.exceptions.CheckViolationError: partialclass(
HTTPException,
status_code=400,
detail="The provided data violates one or more constraints. Please check the input values.",
),
asyncpg.exceptions.DataError: partialclass(
HTTPException,
status_code=400,
detail="Invalid data provided. Please check the input values.",
),
}
)
@wrap_in_class(
ResourceDeletedResponse,
one=True,
Expand Down
28 changes: 18 additions & 10 deletions agents-api/agents_api/queries/agents/get_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@

from beartype import beartype
from sqlglot import parse_one
from fastapi import HTTPException
import asyncpg

from ...autogen.openapi_model import Agent
from ..utils import (
pg_query,
wrap_in_class,
rewrap_exceptions,
partialclass,
)

# Define the raw SQL query
Expand All @@ -35,16 +39,20 @@
""").sql(pretty=True)


# @rewrap_exceptions(
# {
# psycopg_errors.ForeignKeyViolation: partialclass(
# HTTPException,
# status_code=404,
# detail="The specified developer does not exist.",
# )
# }
# # TODO: Add more exceptions
# )
@rewrap_exceptions(
{
asyncpg.exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
asyncpg.exceptions.DataError: partialclass(
HTTPException,
status_code=400,
detail="Invalid data provided. Please check the input values.",
),
}
)
@wrap_in_class(Agent, one=True, transform=lambda d: {"id": d["agent_id"], **d})
@pg_query
@beartype
Expand Down
29 changes: 17 additions & 12 deletions agents-api/agents_api/queries/agents/list_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

from beartype import beartype
from fastapi import HTTPException

import asyncpg
from ...autogen.openapi_model import Agent
from ..utils import (
pg_query,
wrap_in_class,
rewrap_exceptions,
partialclass,
)

# Define the raw SQL query
Expand All @@ -39,17 +41,20 @@
LIMIT $2 OFFSET $3;
"""


# @rewrap_exceptions(
# {
# psycopg_errors.ForeignKeyViolation: partialclass(
# HTTPException,
# status_code=404,
# detail="The specified developer does not exist.",
# )
# }
# # TODO: Add more exceptions
# )
@rewrap_exceptions(
{
asyncpg.exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
asyncpg.exceptions.DataError: partialclass(
HTTPException,
status_code=400,
detail="Invalid data provided. Please check the input values.",
),
}
)
@wrap_in_class(Agent, transform=lambda d: {"id": d["agent_id"], **d})
@pg_query
@beartype
Expand Down
38 changes: 28 additions & 10 deletions agents-api/agents_api/queries/agents/patch_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@

from beartype import beartype
from sqlglot import parse_one
from fastapi import HTTPException
import asyncpg

from ...autogen.openapi_model import PatchAgentRequest, ResourceUpdatedResponse
from ...metrics.counters import increase_counter
from ..utils import (
pg_query,
wrap_in_class,
rewrap_exceptions,
partialclass,
)

# Define the raw SQL query
Expand Down Expand Up @@ -44,16 +48,30 @@
""").sql(pretty=True)


# @rewrap_exceptions(
# {
# psycopg_errors.ForeignKeyViolation: partialclass(
# HTTPException,
# status_code=404,
# detail="The specified developer does not exist.",
# )
# }
# # TODO: Add more exceptions
# )
@rewrap_exceptions(
{
asyncpg.exceptions.ForeignKeyViolationError: partialclass(
HTTPException,
status_code=404,
detail="The specified developer does not exist.",
),
asyncpg.exceptions.UniqueViolationError: partialclass(
HTTPException,
status_code=409,
detail="An agent with this canonical name already exists for this developer.",
),
asyncpg.exceptions.CheckViolationError: partialclass(
HTTPException,
status_code=400,
detail="The provided data violates one or more constraints. Please check the input values.",
),
asyncpg.exceptions.DataError: partialclass(
HTTPException,
status_code=400,
detail="Invalid data provided. Please check the input values.",
),
}
)
@wrap_in_class(
ResourceUpdatedResponse,
one=True,
Expand Down
Loading

0 comments on commit dc0ec36

Please sign in to comment.