Skip to content

Commit

Permalink
Return error when domain is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
radovanZRasa committed Jun 15, 2024
1 parent 7605fe8 commit 39af61e
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 11 deletions.
126 changes: 125 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ opentelemetry-exporter-otlp = "~1.15.0"
grpcio = "1.59.3"
protobuf = "4.25.3"
grpcio-tools = "1.56.2"
pydantic = "2.6.4"

[tool.poetry.dev-dependencies]
pytest-cov = "^4.1.0"
Expand Down
19 changes: 19 additions & 0 deletions rasa_sdk/grpc_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from pydantic import BaseModel, Field

import dataclasses

from enum import Enum


class ResourceNotFoundType(str, Enum):
"""Type of resource that was not found."""
ACTION = "ACTION"
DOMAIN = "DOMAIN"


class ResourceNotFound(BaseModel):
action_name: str = Field(alias="action_name")
message: str = Field(alias="message")
resource_type: ResourceNotFoundType = Field(alias="resource_type")
37 changes: 27 additions & 10 deletions rasa_sdk/grpc_server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import grpc
import utils
import logging
Expand All @@ -10,9 +12,10 @@

from rasa_sdk.constants import DEFAULT_SERVER_PORT, DEFAULT_ENDPOINTS_PATH
from rasa_sdk.executor import ActionExecutor
from rasa_sdk.grpc_exceptions import ResourceNotFound, ResourceNotFoundType
from rasa_sdk.grpc_py import action_webhook_pb2, action_webhook_pb2_grpc
from rasa_sdk.grpc_py.action_webhook_pb2 import WebhookRequest
from rasa_sdk.interfaces import ActionExecutionRejection, ActionNotFoundException
from rasa_sdk.interfaces import ActionExecutionRejection, ActionNotFoundException, ActionMissingDomainException
from rasa_sdk.tracing.utils import (
get_tracer_and_context,
TracerProvider,
Expand All @@ -38,16 +41,14 @@ def __init__(
self.executor = executor

async def webhook(self, request: WebhookRequest, context):
tracer, context, span_name = get_tracer_and_context(
tracer, tracer_context, span_name = get_tracer_and_context(
self.tracer_provider, request
)
with tracer.start_as_current_span(span_name, context=context) as span:
with tracer.start_as_current_span(span_name, context=tracer_context) as span:
utils.check_version_compatibility(request.version)
try:
action_call = MessageToDict(request)
logger.info(f"Received request to run action '{action_call.get('next_action')}'.")
result = None
# result = await self.executor.run(action_call)
action_call = MessageToDict(request, preserving_proto_field_name=True)
result = await self.executor.run(action_call)
except ActionExecutionRejection as e:
logger.debug(e)
body = {"error": e.message, "action_name": e.action_name}
Expand All @@ -56,9 +57,25 @@ async def webhook(self, request: WebhookRequest, context):
return action_webhook_pb2.WebhookResponse()
except ActionNotFoundException as e:
logger.error(e)
body = {"error": e.message, "action_name": e.action_name}
context.set_code(grpc.StatusCode.NOTFOUND)
context.set_details(str(body))
resource_not_found = ResourceNotFound(
action_name=e.action_name,
message=e.message,
resource_type=ResourceNotFoundType.ACTION,
)
body = resource_not_found.json()
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details(body)
return action_webhook_pb2.WebhookResponse()
except ActionMissingDomainException as e:
logger.error(e)
resource_not_found = ResourceNotFound(
action_name=e.action_name,
message=e.message,
resource_type=ResourceNotFoundType.DOMAIN,
)
body = resource_not_found.json()
context.set_code(grpc.StatusCode.NOT_FOUND)
context.set_details(body)
return action_webhook_pb2.WebhookResponse()
if not result:
return action_webhook_pb2.WebhookResponse()
Expand Down

0 comments on commit 39af61e

Please sign in to comment.