Skip to content

Commit

Permalink
fix: Improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jopemachine committed Jan 8, 2025
1 parent c0486d8 commit c66f4e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/ai/backend/manager/api/container_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
from sqlalchemy.ext.asyncio import AsyncSession

from ai.backend.logging import BraceStyleAdapter
from ai.backend.manager.api.exceptions import ContainerRegistryNotFound
from ai.backend.manager.api.exceptions import (
ContainerRegistryWebhookAuthorizationFailed,
InternalServerError,
)
from ai.backend.manager.container_registry.harbor import HarborRegistry_v2
from ai.backend.manager.models.association_container_registries_groups import (
AssociationContainerRegistriesGroupsRow,
Expand Down Expand Up @@ -184,14 +187,14 @@ async def harbor_webhook_handler(request: web.Request, params: Any) -> web.Respo

registry_row = await _get_registry_row_matching_url(db_sess, registry_url, project)
if not registry_row:
raise ContainerRegistryNotFound(
status_code=500,
raise InternalServerError(
extra_msg=f"Harbor webhook triggered, but the matching container registry row not found! (registry_url: {registry_url}, project: {project})",
)

if not _is_authorized_harbor_webhook_request(auth_header, registry_row):
log.warning("Unauthorized Harbor webhook request")
return web.json_response({}, status=401)
raise ContainerRegistryWebhookAuthorizationFailed(
extra_msg=f"Unauthorized webhook request (registry: {registry_row.registry_name}, project: {project})",
)

await _handle_harbor_webhook_event(
root_ctx, event_type, registry_row, project, img_name, resource["tag"]
Expand Down
6 changes: 5 additions & 1 deletion src/ai/backend/manager/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def __init__(
if object_name:
self.object_name = object_name
self.error_title = f"No such {self.object_name}."
self.status_code = status_code
super().__init__(extra_msg, extra_data, **kwargs)


Expand Down Expand Up @@ -196,6 +195,11 @@ class GraphQLError(BackendError, web.HTTPBadRequest):
error_title = "GraphQL-generated error."


class ContainerRegistryWebhookAuthorizationFailed(BackendError, web.HTTPUnauthorized):
error_type = "https://api.backend.ai/probs/webhook/auth-failed"
error_title = "Container Registry Webhook authorization failed."


class InstanceNotFound(ObjectNotFound):
object_name = "agent instance"

Expand Down

0 comments on commit c66f4e5

Please sign in to comment.