Skip to content

Commit

Permalink
feat: error handler 형식 json 으로 반환하도록 변환 #13
Browse files Browse the repository at this point in the history
  • Loading branch information
jieun-lim committed Aug 22, 2024
1 parent 6c447ca commit f106952
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
9 changes: 4 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import warnings
import os
from flask import Flask, request, jsonify, Response
from flask import Flask, request, jsonify, Response, abort
from flask_cors import CORS
from dotenv import load_dotenv
from pdf_retriever import pdf_retriever
Expand All @@ -28,8 +28,6 @@
datefmt='%Y-%m-%d %H:%M:%S'
)

# flask 앱 정의
app = Flask(__name__)

# API 키 로드하기
print("환경변수 로드 ")
Expand All @@ -39,7 +37,6 @@
KAKAO_MAP_API_KEY = os.environ.get("KAKAO_MAP_API_KEY1")
WEATHER_API_KEY = os.environ.get("WEATHER_API_KEY")


# LLM 변수 정의
MAX_TOKENS_OUTPUT = 200
STREAM_TOKEN_SIZE = 30
Expand Down Expand Up @@ -207,7 +204,9 @@ def error_handle(): # 대화의 타이틀 생성 #(params)
raise BadRequest("No request body")
elif 'content' not in params or not params['content'].strip(): # json = {'msg': "..."} or json = {'content': ""}
raise BadRequest("No content field in request body or value for content is empty")
return jsonify({"result": f"no 400 error:{params['content']}"})
# abort(400, description="No request body ---- ")
#raise BadRequest()
return jsonify({"result": f"no error:{params['content']}"})


if __name__ == '__main__':
Expand Down
19 changes: 13 additions & 6 deletions error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@
에러 핸들러 정의
"""

from flask import jsonify

from flask import jsonify, make_response
import logging
# 로깅 설정
logging.basicConfig(
filename='./logging/error_log.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
def register_error_handlers(app):
# 404 에러 핸들러 정의
@app.errorhandler(404)
def not_found_error(error):
return jsonify({"error": "Resource not found"}), 404
return jsonify({'error': "Resource not found", 'code': 404, "description": f"{error.description}"})

# 400 에러 핸들러 정의
@app.errorhandler(400)
def bad_request_error(error):
return jsonify({"error": "Bad request"}), 400
return jsonify({'error': "Bad request", 'code': 400, "description": f"{error.description}"})


# 500 에러 핸들러 정의
@app.errorhandler(500)
def internal_error(error):
return jsonify({"error": "Internal server error"}), 500
return jsonify({'error': "Internal server error", 'code': 500, "description": f"{error.description}"})

0 comments on commit f106952

Please sign in to comment.