| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- """
- api_exception -
- auther: tanlie
- date: 2026/8/23
- """
- from flask import request, json
- from werkzeug.exceptions import HTTPException
- class APIException(HTTPException):
- code = 500
- msg = 'sorry, we made a mistake (* ̄︶ ̄)!'
- error_code = 999
- def __init__(self, msg=None, code=None, error_code=None, headers=None):
- if code:
- self.code = code
- if error_code:
- self.error_code = error_code
- if msg:
- self.msg = msg
- super(APIException, self).__init__(msg, None)
- def get_body(self, environ=None, scope=None, *args, **kwargs):
- body = dict(
- msg=self.msg,
- error_code=self.error_code,
- request=request.method + ' ' + self.get_url_no_param()
- )
- text = json.dumps(body)
- return text
- def get_headers(self, environ=None, scope=None, *args, **kwargs):
- """Get a list of headers.
- Werkzeug/Flask may pass ``environ`` and ``scope`` positional arguments in
- newer versions, so accept them for compatibility.
- """
- return [("Content-Type", "application/json")]
- @staticmethod
- def get_url_no_param():
- full_path = str(request.full_path)
- main_path = full_path.split('?')
- return main_path[0]
|