fix: error handling

deploy/dev
stream 7 months ago
parent a3074aa0a5
commit 1523e1db74

@ -3,7 +3,6 @@ from collections.abc import Sequence
from flask_login import current_user from flask_login import current_user
from flask_restful import Resource, reqparse from flask_restful import Resource, reqparse
from opik.rest_api import BadRequestError
from controllers.console import api from controllers.console import api
from controllers.console.app.error import ( from controllers.console.app.error import (
@ -136,13 +135,23 @@ class InstructionGenerateApi(Resource):
from models import App, db from models import App, db
from services.workflow_service import WorkflowService from services.workflow_service import WorkflowService
app = db.session.query(App).filter(App.id == args["flow_id"]).first() app = db.session.query(App).filter(App.id == args["flow_id"]).first()
if not app:
return { "error": f"app {args['flow_id']} not found" }, 400
workflow = WorkflowService().get_draft_workflow(app_model=app) workflow = WorkflowService().get_draft_workflow(app_model=app)
nodes:Sequence = workflow.graph_dict["nodes"] nodes:Sequence = workflow.graph_dict["nodes"]
node: dict = [node for node in nodes if node["id"] == args["node_id"]][0] node = [node for node in nodes if node["id"] == args["node_id"]]
if not node: if len(node) == 0:
raise BadRequestError(f"node {args['node_id']} not found") return { "error": f"node {args['node_id']} not found" }, 400
match node_type:=node["data"]["type"]: node_type=node[0]["data"]["type"]
case "llm", "agent": match node_type:
case "llm":
return LLMGenerator.generate_rule_config(
current_user.current_tenant_id,
instruction=args["instruction"],
model_config=args["model_config"],
no_variable=True
)
case "agent":
return LLMGenerator.generate_rule_config( return LLMGenerator.generate_rule_config(
current_user.current_tenant_id, current_user.current_tenant_id,
instruction=args["instruction"], instruction=args["instruction"],
@ -157,7 +166,7 @@ class InstructionGenerateApi(Resource):
code_language=args["language"], code_language=args["language"],
) )
case _: case _:
raise BadRequestError(f"invalid node type: {node_type}") return { "error": f"invalid node type: {node_type}"}
if args["node_id"] == "" and args["current"] != "": # For legacy app without a workflow if args["node_id"] == "" and args["current"] != "": # For legacy app without a workflow
return LLMGenerator.instruction_modify_legacy( return LLMGenerator.instruction_modify_legacy(
tenant_id=current_user.current_tenant_id, tenant_id=current_user.current_tenant_id,
@ -177,7 +186,7 @@ class InstructionGenerateApi(Resource):
model_config=args["model_config"], model_config=args["model_config"],
ideal_output=args["ideal_output"], ideal_output=args["ideal_output"],
) )
raise BadRequestError("incompatible parameters") return { "error": "incompatible parameters" }, 400
except ProviderTokenNotInitError as ex: except ProviderTokenNotInitError as ex:
raise ProviderNotInitializeError(ex.description) raise ProviderNotInitializeError(ex.description)
except QuotaExceededError: except QuotaExceededError:

Loading…
Cancel
Save