Skip to content

Commit

Permalink
Merge pull request #168 from barrierye/master
Browse files Browse the repository at this point in the history
AgentRuntime优化错误码
  • Loading branch information
seiriosPlus authored Mar 7, 2024
2 parents 02ed9c8 + 7ba7756 commit 79c7d3f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions appbuilder/core/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,12 @@ def create_flask_app(self, url_rule="/chat"):
def handle_bad_request(e):
return {"code": 400, "message": f'{e}', "result": None}, 400

@app.errorhandler(RuntimeError)
@app.errorhandler(Exception)
def handle_bad_request(e):
return {"code": 1000, "message": f'{e}', "result": None}, 200
if hasattr(e, "code"):
return {"code": e.code, "message": str(e), "result": None}, 200
else:
return {"code": 500, "message": "Internal Server Error", "result": None}, 200

def warp():
# 根据component是否lazy_certification,分成两种情况:
Expand Down Expand Up @@ -305,7 +308,8 @@ def gen_sse_resp(stream_message):
}, ensure_ascii=False) + "\n\n"
self.user_session._post_append()
except Exception as e:
err_resp = {"code": 1000, "message": str(e), "result": None}
code = 500 if not hasattr(e, "code") else e.code
err_resp = {"code": code, "message": str(e), "result": None}
logging.error(f"[request_id={request_id}, session_id={session_id}] err={e}", exc_info=True)
yield "data: " + json.dumps(err_resp, ensure_ascii=False) + "\n\n"
return Response(
Expand All @@ -322,7 +326,7 @@ def gen_sse_resp(stream_message):
}
except Exception as e:
logging.error(f"[request_id={request_id}, session_id={session_id}] err={e}", exc_info=True)
raise RuntimeError(e)
raise e

app.add_url_rule(url_rule, 'chat', warp, methods=['POST'])
return app
Expand Down

0 comments on commit 79c7d3f

Please sign in to comment.