Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add SkyvernHTTPException base exception to automate known error handling in agent service #317

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions skyvern/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from fastapi import status


class SkyvernException(Exception):
def __init__(self, message: str | None = None):
self.message = message
super().__init__(message)


class SkyvernHTTPException(SkyvernException):
def __init__(self, message: str | None = None, status_code: int = status.HTTP_400_BAD_REQUEST):
self.status_code = status_code
super().__init__(message)


class InvalidOpenAIResponseFormat(SkyvernException):
def __init__(self, message: str | None = None):
super().__init__(f"Invalid response format: {message}")
Expand All @@ -22,9 +31,9 @@ def __init__(self, proxy_location: str | None = None):
super().__init__(f"Unknown proxy location: {proxy_location}")


class TaskNotFound(SkyvernException):
class TaskNotFound(SkyvernHTTPException):
def __init__(self, task_id: str | None = None):
super().__init__(f"Task {task_id} not found")
super().__init__(f"Task {task_id} not found", status_code=status.HTTP_404_NOT_FOUND)


class ScriptNotFound(SkyvernException):
Expand Down Expand Up @@ -97,9 +106,9 @@ def __init__(self, block_type: str) -> None:
super().__init__(f"Unknown block type {block_type}")


class WorkflowNotFound(SkyvernException):
class WorkflowNotFound(SkyvernHTTPException):
def __init__(self, workflow_id: str) -> None:
super().__init__(f"Workflow {workflow_id} not found")
super().__init__(f"Workflow {workflow_id} not found", status_code=status.HTTP_404_NOT_FOUND)


class WorkflowRunNotFound(SkyvernException):
Expand All @@ -119,9 +128,9 @@ def __init__(self, parameter_key: str, workflow_id: str, workflow_run_id: str) -
)


class WorkflowParameterNotFound(SkyvernException):
class WorkflowParameterNotFound(SkyvernHTTPException):
def __init__(self, workflow_parameter_id: str) -> None:
super().__init__(f"Workflow parameter {workflow_parameter_id} not found")
super().__init__(f"Workflow parameter {workflow_parameter_id} not found", status_code=status.HTTP_404_NOT_FOUND)


class FailedToNavigateToUrl(SkyvernException):
Expand Down Expand Up @@ -163,14 +172,17 @@ def __init__(self) -> None:
super().__init__("BrowserState is missing the main page")


class OrganizationNotFound(SkyvernException):
class OrganizationNotFound(SkyvernHTTPException):
def __init__(self, organization_id: str) -> None:
super().__init__(f"Organization {organization_id} not found")
super().__init__(f"Organization {organization_id} not found", status_code=status.HTTP_404_NOT_FOUND)


class StepNotFound(SkyvernException):
class StepNotFound(SkyvernHTTPException):
def __init__(self, organization_id: str, task_id: str, step_id: str | None = None) -> None:
super().__init__(f"Step {step_id or 'latest'} not found. organization_id={organization_id} task_id={task_id}")
super().__init__(
f"Step {step_id or 'latest'} not found. organization_id={organization_id} task_id={task_id}",
status_code=status.HTTP_404_NOT_FOUND,
)


class FailedToTakeScreenshot(SkyvernException):
Expand Down
5 changes: 5 additions & 0 deletions skyvern/forge/api_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from starlette_context.middleware import RawContextMiddleware
from starlette_context.plugins.base import Plugin

from skyvern.exceptions import SkyvernHTTPException
from skyvern.forge import app as forge_app
from skyvern.forge.sdk.core import skyvern_context
from skyvern.forge.sdk.core.skyvern_context import SkyvernContext
Expand Down Expand Up @@ -73,6 +74,10 @@ def start_scheduler() -> None:

LOG.info("Server startup complete. Skyvern is now online")

@app.exception_handler(SkyvernHTTPException)
async def handle_skyvern_http_exception(request: Request, exc: SkyvernHTTPException) -> JSONResponse:
return JSONResponse(status_code=exc.status_code, content={"detail": exc.message})

@app.exception_handler(Exception)
async def unexpected_exception(request: Request, exc: Exception) -> JSONResponse:
LOG.exception("Unexpected error in agent server.", exc_info=exc)
Expand Down
Loading