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.
This commit is contained in:
parent
53656dcc1f
commit
a21582589c
1 changed files with 24 additions and 1 deletions
|
|
@ -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)):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue