From a21582589c575e9de2b03ecc381c0a39e36b444d Mon Sep 17 00:00:00 2001 From: gustavoschaedler Date: Wed, 5 Jul 2023 16:22:22 +0100 Subject: [PATCH] import custom.py: Add HTTPExceptionWithTraceback class This commit adds a new class `HTTPExceptionWithTraceback` that extends `HTTPException` from the `fastapi` module. This new class includes a `traceback` attribute. Additionally, in the `extract_class_info` method of the `CustomComponent` class, a try-except block is added to catch any syntax errors raised by parsing the provided code. If a syntax error is encountered, an HTTPException is raised with the error message and traceback. --- .../langflow/interface/tools/custom.py | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/backend/langflow/interface/tools/custom.py b/src/backend/langflow/interface/tools/custom.py index 8f5c9fcd8..6e124dc52 100644 --- a/src/backend/langflow/interface/tools/custom.py +++ b/src/backend/langflow/interface/tools/custom.py @@ -1,4 +1,5 @@ import ast +import traceback from typing import Callable, Optional from langflow.interface.importing.utils import get_function @@ -8,6 +9,14 @@ from pydantic import BaseModel, validator from langflow.utils import validate from langchain.agents.tools import Tool +from fastapi import HTTPException + + +class HTTPExceptionWithTraceback(HTTPException): + def __init__(self, status_code, detail=None, traceback=None): + super().__init__(status_code, detail) + self.traceback = traceback + class Function(BaseModel): code: str @@ -153,7 +162,21 @@ class CustomComponent(BaseModel): return output_list def extract_class_info(self): - module = ast.parse(self.code) + try: + module = ast.parse(self.code) + except SyntaxError as err: + raise HTTPException( + status_code=400, + detail={ + 'error': err.msg, + 'traceback': traceback.format_exc() + } + ) + # raise HTTPExceptionWithTraceback( + # status_code=400, + # detail=err.msg, + # traceback=traceback.format_exc() + # ) from err for node in module.body: if isinstance(node, (ast.Import, ast.ImportFrom)):