플라스크에서 AOP 처리하기

이동욱

2021/11/24

플라스크에서 AOP 처리하기


exception.py

class InvalidAPIUsage(Exception):
    status_code = 400

    def __init__(self, message, status_code=None, payload=None):
        super().__init__()
        self.message = message
        if status_code:
            self.status_code = status_code
        self.payload = payload

    def to_dict(self):
        rv = dict(self.payload or ())
        rv['message'] = self.message
        return rv

app.py

@app.errorhandler(InvalidAPIUsage)
def invalid_api_usage(e):
    return jsonify(error=str(e.to_dict()))

@app.route("/")
def index():
    raise InvalidAPIUsage("No user id provided!")

참고 문헌

>> Home